loop terminations

Hey!

Can any1 settle a discussion for me?

Myself and a colleague are *ahem* "discussing" why some loops in C finish with a semi-colon but others do not. eg if and do/while. Can anyone settle this for us. I do remember reading an explanation somewhere before but i cant remember what it was!

It's a matter of the compiler knowing where statements end, really. With most statements, you'll need the semi-colon to mark the end. When you're using a compound statement (using curly braces, '{' and '}'), it's pretty much clear where the statement ends, isn't it?

Code:

if (true) {
/* do something */
}

That also explains why the do .. while loop construct -does- need a semi-colon: the curly brace isn't the last token in the statement.

Code:

do {
/* something */
} while (this is true);

"A poor programmer is he who blames his tools."
http://analyser.oli.tudelft.nl/