The
for
loop should have a body around braces if the body consists of more than one line.
For example:
1 2 3 4
|
for (i = 1; i < 10; ++i) {
foo1();
foo2();
}
|
When the body consists of only a single line the braces may be omitted:
1 2
|
for (i = 1; i < 10; ++i) foo1();
foo2(); /* Will be executed when the loop stops. */
|
In the code you showed above, you did not provide braces. The single ';' implies a null statement. It does nothing.
It also implies, in your case, the end of the body of the for loop. So, it will just iterate through the null statement and nothing more, hence it prints nothing, because the body is missing.
The condition
i <= 10
becomes false when
i
reaches the value 11. When it does (reach 11) it exits the loop and executes the next statement it encounters. Which is your
cout statement. It then prints the correct value of i which is indeed 11.
In the event that you do remove the null statement, the
cout statement becomes the loop's body. So it executes that until the condition is false. It only prints up to 10 because the loop is designed not to iterate further if the value is greater than 10.
However, once the loop exits the value of i, would be 11, but it doesn't print that because there is no cout statement after the body. You could add another
cout statement outside the loop to test this, which is what you did in the former case - the cout was outside the body of the loop.
I hope I was perspicuous enough.
Take my advice, and take some C/C++ course, a teacher will teach far better things than us forum members.
Alternatively, buy some book. It will be quite handy and answer all basic questions.