The for statement can be divided into three separate parts, as shown in Table 5.3.
Table 5.3 for Loop Elements
Syntax Name When Executed Contents
for-init-statement Before any other element of the for statement or the substatement. Often used to initialize loop indices. It can contain expressions or declarations.
expression1 Before execution of a given iteration of the loop, including the first iteration. An expression that evaluates to an integral type or a class type that has an unambiguous conversion to an integral type.
expression2 At the end of each iteration of the loop; expression1 is tested after expression2 is evaluated. Normally used to increment loop indices.
The for-init-statement is commonly used to declare and initialize loop-index variables. The expression1 is often used to test for loop-termination criteria. The expression2 is commonly used to increment loop indices.
The for statement executes the statement repeatedly until expression1 evaluates to zero. The for-init-statement, expression1, and expression2 fields are all optional.
The following for loop:
for( for-init-statement; expression1; expression2 )
is equivalent to the following while loop:
while( expression1 )