For Loop Syntax Question

Hey all,

I have a question regarding this syntax of a for loop, or atleast to find out if this is valid code and why it produces the value it does. I can't find anything on it by searching.

1
2
3
4
5

int i;
for(i = 1; i <= 10; i++);
  cout << i << " ";


As you can see, it is a basic for loop, however, I ended the for function with a ';' and I cout the numerical value in the next line.

The output to screen is: 11. Now, I'm assuming this value comes from the for loop "looping" as it should but just doesn't show the iteration on screen, (1,2,3,4..etc.), just the value "11". But when I remove the ';' from the function it iterates on the screen and ends at 10, not 11.

Could someone please explain why this is? Meaning, why does one way show only 11 and the other way counts to 10. Also, is ending the for loop with a semi-colon proper? I never do it personally, but when experimenting, my compiler never spit out a warning or anything.

Thanks all
It is perfectly valid code - it is just not what you wanted to do.

A for loop consists of the for part which looks like this:
for(initialization; test part; end of evry loop pass instruction)
Notice the lack of semi-colon.

That part is then followed by 1 statement. This statement can consist of one actual statement - or
a block of any number of statements in { } which is treated as 1 statement.
In C++ a semicolon is an empty stemement.
So rewriting your loop slightly - this is what you have done:
1
2
3
4
5
6
int i;
for(i = 1; i <= 10; i++)
{
    ; //an empty do nothing statement
}
  cout << i << " "; //This is not in the loop 



Thank you guestgulkan. You did an excellent job explaining that.

Why does it show 11 and not 10 though? That part is confusing me a bit. Why would the semi-colon effect that?
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.
Last edited on
Hi unoriginal and thank you.

Yes, you explained it clearly and helped immensely. I knew 11 was the correct value output, but I never thought of it being able to be read outside the statement body with additional cout statements. Makes total sense though. Thank you very much.

As for taking courses and buying books, I own 6 C++ books for learning (which are all excellent) and am involved in courses. However, not much can be said about the instructors I'm learning from save a couple of them and the only issues I have with the books is sometimes the explainations leave quite a bit out in regards to the technical why's and how's things happen in C++ and having a very technical mind, I need to know how something works or why it's doing what it does to really understand it.

Thanks all of you for your help. It's much appreciated.



Topic archived. No new replies allowed.