for [C++ Reference]
Keywords: keywords,for
Date: 2008-10-14T15:42:04-0700

[[for]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- fr
- it
- jp
- pl
- ru
- tr

## for

Syntax:

        for( initialization; test-condition; increment ) {
        statement-list;
        }

The for construct is a general looping mechanism consisting of 4 parts:

      1. the initialization, which consists of 0 or more comma-delimited variable
         initialization statements
      2. the test-condition, which is evaluated to determine if the execution of
         the for loop will continue
      3. the increment, which consists of 0 or more comma-delimited statements that
         increment variables
      4. and the statement-list, which consists of 0 or more statements that will
         be executed each time the loop is executed.

For example:

         for( int i = 0; i < 10; i++ ) {
           cout << "i is " << i << endl;
         }
         int j, k;
         for( j = 0, k = 10;
              j < k;
              j++, k-- ) {
           cout << "j is " << j << " and k is " << k << endl;
         }
         for( ; ; ) {
           // loop forever!
         }

Related Topics: break, continue, do, if, while

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •