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