Partager via


The for Statement 

Executes statement and loop-expression repeatedly until cond-expression becomes false.

for ( init-expression ; cond-expression ; loop-expression )
      statement 

Remarks

Use the for statement to construct loops that must execute a specified number of times.

The for statement consists of three optional parts, as shown in the following table.

for Loop Elements

Syntax Name When Executed Contents

init-expression

Before any other element of the for statement. init-expression is executed only once. Control then passes to cond-expression.

Often used to initialize loop indices. It can contain expressions or declarations (C++ only).

cond-expression

Before execution of each iteration of statement, including the first iteration. statement is executed only if cond-expression evaluates to true (nonzero).

An expression that evaluates to an integral type or a class type that has an unambiguous conversion to an integral type. Normally used to test for loop-termination criteria.

loop-expression

At the end of each iteration of statement. After loop-expression is executed, cond-expression is evaluated.

Normally used to increment loop indices.

For example:

// for_statement1.cpp
#include <stdio.h>
int main() {
int i;
for ( i = 0 ; i < 2 ; i++ ) 
   printf_s( "\n%d\n",i );
}

The preceding for loop is equivalent to the following while loop:

// for_statement2.cpp
#include <stdio.h>
int main()
{
   int i = 0;
   while ( i < 2 )
      printf_s( "%d\n", i++ );
}

init-expression and loop-expression can contain multiple statements separated by the comma operator. For example:

// for_statment3.cpp
#include <stdio.h>
int main()
{
   int i, j;
   for ( i = 5, j = 10 ; i + j < 20; i++, j++ )
      printf_s( "\n i + j = %d", (i + j) );
}

A for loop terminates when a break, return, or goto (to a labeled statement outside the for loop) within statement is executed. A continue statement in a for loop terminates only the current iteration.

If cond-expression is omitted, it is considered true and the for loop will not terminate without a break, return, or goto within statement.

A convenient way to specify an infinite loop using the for statement is:

for( ; ; )
{
    // Statements to be executed.
}

Although the three fields of the for statement are normally used for initialization, testing for termination, and incrementing, they are not restricted to these uses. For example, the following code prints the numbers 1 to 5. In this case, statement is the null statement:

// for_statement4.cpp
#include <stdio.h>
int main()
{
    int i;
    for( i = 0; i < 5; printf_s("%d\n", i), i++)
        ;
}

for Loops and the C++ Standard

The C++ standard says that a variable declared in a for loop shall go out of scope after the for loop ends. For example:

for (int i = 0 ; i < 5 ; i++) {
   // do something
}
// i is now out of scope under /Za or /Zc:forScope

By default, under /Ze, a variable declared in a for loop remains in scope until the for loop's enclosing scope ends.

/Zc:forScope enables standard behavior of variables declared in for loops without needing to specify /Za.

It is also possible to use the scoping differences of the for loop to redeclare variables under /Ze as follows:

// for_statement5.cpp
int main(){
   int i = 0;   // hidden by var with same name declared in for loop
   for ( int i = 0 ; i < 3; i++ ) {}

   for ( int i = 0 ; i < 3; i++ ) {}
}

This more closely mimics the standard behavior of a variable declared in a for loop, which requires variables declared in a for loop to go out of scope after the loop is done. When a variable is declared in a for loop, the compiler internally promotes it to a local variable in the for loop's enclosing scope even if there is already a local variable with the same name.

See Also

Reference

Iteration Statements (C++)
C++ Keywords
The while Statement
The do-while Statement (C++)