The for statement

A for statement lets you do the following:

Read syntax diagramSkip visual syntax diagramfor statement syntax
 
>>-for---------------------------------------------------------->
 
>--(--+-------------+--;--+-------------+--;--+-------------+--)-->
      '-expression1-'     '-expression2-'     '-expression3-'
 
>--statement---------------------------------------------------><
 

expression1 is the initialization expression. It is evaluated only before the statement is processed for the first time. You can use this expression to initialize a variable. You can also use this expression to declare a variable, provided that the variable is not declared as static (it must be automatic and may also be declared as register). If you declare a variable in this expression, or anywhere else in statement, that variable goes out of scope at the end of the for loop. If you do not want to evaluate an expression prior to the first iteration of the statement, you can omit this expression.

expression2 is the conditional expression. It is evaluated before each iteration of the statement. C expression2 must be of arithmetic or pointer type.  C++ expression3 must be convertible to type bool.

If it evaluates to false (or 0 in C), the statement is not processed and control moves to the next statement following the for statement. If expression2 does not evaluate to false, the statement is processed. If you omit expression2, it is as if the expression had been replaced by true, and the for statement is not terminated by failure of this condition.

expression3 is evaluated after each iteration of the statement. This expression is often used for incrementing, decrementing, or assigning to a variable. This expression is optional.

A break, return, or goto statement can cause a for statement to end, even when the second expression does not evaluate to false. If you omit expression2, you must use a break, return, or goto statement to end the for statement.

Related information



[ Top of Page | Previous Page | Next Page | Contents | Index ]