for statement | For loop statement |
statement := for ( for-init-stmt [condition] ; [expression] ) statement for-init-stmt ::= expression-stmt | simple-decl condition ::= expression | type-specifier-seq declarator = assignment-expr |
The for loop is used for bounded loops and for unbounded loops that have well-defined iterations. Execution starts with for-init-stmt, which can be an expression statement or a declaration. (Note that the syntax for expression-stmt and simple-decl both include a terminating semicolon.)
condition is then evaluated. If condition evaluates to true, statement is executed. The iteration expression is then evaluated, and the condition is tested again. When condition is false, the loop ends and control passes to the statement following the end of the for statement.
Declarations in for-int-stmt and condition are in the same scope as expression and statement. A continue statement inside statement transfers control to the evaluation of the iteration expression.
for (int i = 0; i < 10; ++i) cout << i << '\n'; for (node* n = head; n != 0 ; n = n->next) link_node(n);
break, continue, do, expression, statement, while, Chapter 4