Confuzed with braces please!

Nov 27, 2010 at 11:55pm
Hi, could someone please tell me the difference between the two below peices of code:

cout << "\nCounting forward:\n";
for (int i = 0; i < 10; ++i)
cout << i << " ";

and

cout << "\nCounting forward:\n";
for (int i = 0; i < 10; ++i)
{
cout << i << " ";
}

If i put in the braces does that mean everything inside it is carried out, so i can put more than one line, and if i dont put braces then only the 1 line below is part of the for loop

also...

everytime it checks the test and carries out the cout statement, how does it know to put the second test on the line below (and 3rd, and 4th, and 5th...), because im not using "\n" or "endl",
Nov 28, 2010 at 12:44am
There is no difference in this case. You don't need braces if there is only one line within the loop.

If i put in the braces does that mean everything inside it is carried out, so i can put more than one line, and if i dont put braces then only the 1 line below is part of the for loop

That is correct.
Nov 28, 2010 at 1:02am
Thanks for replying

everytime the for loop counts forward, how does it know to put each new number on a different line?
Nov 28, 2010 at 11:42am
??? :)
Nov 28, 2010 at 6:04pm
anyone?
Nov 28, 2010 at 6:24pm
afaik it doesn't :p

the output of your prog is this :

Counting forward:
0 1 2 3 4 5 6 7 8 9 
Nov 28, 2010 at 6:42pm
ha, how daft of me. Thanks
Topic archived. No new replies allowed.