I have two while loops. They both should count to 10 from what I see. But one counts to 11. It seems to me like they should run exactly the same. The only difference is where the cout statement is.
It seems important for me to understand why one runs different from the other.
This loop counts 1-10:
1 2 3 4 5 6 7
int i = 1;
while (i <= 10)
{
cout << i << "\n";
i++;
}
This loop counts 1-11:
1 2 3 4 5 6 7
int i = 1;
while (i <= 10)
{
i++;
cout << i << "\n";
}
So the question is why? If i==11 how does it run one more time?
This loop counts 1-11:
The printed output is 2 to 11.
The condition (i <= 10) is checked before entering the loop. It doesn't control what happens inside the body of the loop. In the second example, i is changed before reaching the cout statement.