Question: I'm having a little trouble figuring out a nested for loop here. Here's the problem: 

> 
> 3. The population of Ireland is 4.8 million and growing at a rate of 7% per year. Write a program to determine and display the population
>  in 10 years time. Your program should also display a count of the
>  number of years that the population is predicted to exceed 5 million.
> 
> 
> 

And here's what I've coded so far:

```
    double pop = 4.8;
    int years = 0;
    for (int i = 0; i < 10; i++)
    {
        pop += (pop / 100) * 7;

        for (int j = 0; pop >5; j++)
        {
            years += j;
        }
    }

    Console.WriteLine("Pop in year 2030 is " + Math.Round(pop, 2) + " million and the population will be over 5 million for " + years + " years.");
}

```

Now to be clear, I need the sum of the years in which population exceeds 5(million) (the answer is 10) and I must only use for loops to solve this problem. I'm thinking the answer is a nested for loop, but have tried and tried with no success.

Can anyone help me on where I've screwed this up?

Answer: 
Look at your inner loop.

```
    for (int j = 0; pop >5; j++)
    {
        years += j;
    }

```

Your condition being `pop > 5`, you need `pop` to shrink if you ever want to exit the loop. But the body of the loop never alters `pop`, so if it's greater than 5, you'll loop forever.

The problem definition suggests that you don't need an inner loop at all. Just check `pop`, and if it's greater than 5, increment `years`.

```
    if (pop > 5)
    {
         ++years;
    }

```

If you're really under such an insane restriction that you can't use `if`, you could do something boneheaded like create a `for` loop that only runs once if your other condition is right.

```
    for (int j = 0; j < 1 && pop > 5; ++j)
    {
        ++years;
    }

```

but no sane person does this.
If I well understand your question.
Technically if you add 7% each year your program should look like this 

```
bool passed = true;
double pop = 4.8;
int year = 0;
for (int i=0; i<10; i++)
{
 pop *= 1.07;
if (pop > 5 && passed)
{
 year = i;
 passed = false;
}
}

```