for (initializationExpression; loopCondition; stepExpression)
{
// statements ...
}
Note that after evaluate initializationExpression,
loopCondition is tested. If it fails then
statements will not be executed.
while (symbol *ptr = search(name)) {
// ptr visible only here
}
if (symbol *ptr = search(name)) {
// ptr visible only here
}
In C++ and C99, scope of the variables declared (and normally
initialized together, usually are looping counters) inside
initializationExpression are within the
for loop body. Similarly, object defined within the
while and if condition is visible only
within the associated statement or statement block.
do while loop does not support an object definition
because the condition is not evaluated until after the statement
or statement block is initially executed.
do {
// statements ...
} while (int i = GetInt()); // error: declaration cannot within do while condition
do
{
int i;
// statements ...
} while (i = GetInt()); // logical error: i initialized again and again
int i;
do
{
// statements ...
} while (i = GetInt()); // ok
Index