A for statement lets you do the following:
Arguments to the for statement are:
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. 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 controlling
part. It is evaluated before each iteration of the statement.
It must evaluate to a scalar type. If it evaluates to 0 (zero), the statement is not processed and control moves to the next statement following the for statement. If expression2 does not evaluate to 0, the statement is processed. If you omit expression2, it is as if the expression had been replaced by a nonzero constant, and the for statement is not terminated by failure of this condition. |
expression3 | Is evaluated after each iteration of the statement. You can use this expression to increase, decrease, or reinitialize 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 0. If you omit expression2, you must use a break, return, or goto statement to end the for statement.
In C++ programs, you can also use expression1 to declare a variable as well as initialize it. If you declare a variable in this expression, the variable has the same scope as the for statement and is not local to the for statement.
break Statement
goto Statement
return Statement
Examples
Using the for Statement