The for statement
- Evaluate an expression before the first iteration of the statement (initialization)
- Specify an expression to determine whether or not the statement should be processed (the condition)
- Evaluate an expression after each iteration of the statement (often used to increment for each iteration)
- Repeatedly process the statement if the controlling part does
not evaluate to
false(or0in C).
for statement syntax
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.
expression2 must be of arithmetic
or pointer type.
expression2 must
be convertible to type bool.
If expression2 evaluates to
0
or 
false
, 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.
Examples of for statements
The following for statement
prints the value of count 20 times.
The for statement initially sets the value of count to 1.
After each iteration of the statement, count is incremented.
int count;
for (count = 1; count <= 20; count++)
printf("count = %d\n", count);The following
sequence of statements accomplishes the same task. Note the use of
the while statement instead of the for statement.
int count = 1;
while (count <= 20)
{
printf("count = %d\n", count);
count++;
}The following for statement does
not contain an initialization expression:
for (; index > 10; --index)
{
list[index] = var1 + var2;
printf("list[%d] = %d\n", index,
list[index]);
}The following for statement will
continue running until scanf receives the letter e:
for (;;)
{
scanf("%c", &letter);
if (letter == '\n')
continue;
if (letter == 'e')
break;
printf("You entered the letter %c\n", letter);
}for statement contains
multiple initializations and increments. The comma operator makes
this construction possible. The first comma in the for expression
is a punctuator for a declaration. It declares and initializes two
integers, i and j. The second comma,
a comma operator, allows both i and j to
be incremented at each step through the loop. for (int i = 0,
j = 50; i < 10; ++i, j += 50)
{
cout << "i = " << i << "and j = " << j
<< endl;
}The following example shows a nested for statement.
It prints the values of an array having the dimensions [5][3].
for (row = 0; row < 5; row++)
for (column = 0; column < 3; column++)
printf("%d\n",
table[row][column]);The outer statement is
processed as long as the value of row is less than 5.
Each time the outer for statement is executed, the
inner for statement sets the initial value of column to
zero and the statement of the inner for statement
is executed 3 times. The inner statement is executed
as long as the value of column is less than 3.