June 24th, 2013, 04:33 PM
Originally Posted by 046
for them, is it more correct to consider them
the statement to be executed once at the beginning of the whole loop
the statement to be executed at the end of each loop?
Not quite, because a statement
and an expression
are not the same thing. An expression has both value and type, an statement may not have either.
Each of the three elements may be any valid expression, including function calls. I would suggest that any expression involving scanf() is probably ill advised. It is generally best to keep it simple for maintainability and clarity. Any expression involving a function with side-effects as a control for a for-loop is probably a bad idea.
Not all code you don't understand is good
code - often the reverse.
for( e1; e2; e3 )
is exactly equivalent to
while( e2 )
Generally id e1 and e3 are not related to e2 (in the sense that the are expressions with variables in common), then the second form should be used. Its a style thing however; you can break that guideline, but if you are producing code that others must work on or maintain, no one will thank you for "clever" code.