The for-Statement

  1. Besides the while-loop and the do-while-loop, C++ has another looping mechanism called the for-loop.

  2. The syntax for the for-loop is:

    for (Initialization_Action; Boolean_Expression; Update_Action)
        Body_Statement

    The initialization action is processed first. Then the Boolean expression is evaluated. If the expression is true then the body statement is executed. After that the update action is executed. The loop repeats by evaluating the Boolean expression again, etc. If the Boolean expression is found to be false, the program exits the loop.

  3. The for-loop is equivalent to the following while-loop:

    Initialization Action;
    while (Boolean_Expression)
    {
        Body_Statement
        Update_Action;
    }

  4. Since the stopping condition is tested before the first iteration, the body of the for-loop may execute zero times.

  5. The three expression at the start of a for-statement are separated by two and only two semicolons.

  6. The initialization action may include declaration of variables. These variables are required by the ANSI C++ standard to be local to the block of the the for-loop. The initialization action may contain multiple statements separated by commas, These statements are executed once and only once before the start of the first loop iteration.

  7. The Boolean expression must be a single Boolean expression which must evaluate either to true or false. The expression is tested at the beginning of each iteration.

  8. The update part may contain more than one statement separated by commas. They are executed after the body statements are executed.

  9. The following is a simple example illustrating how a for-loop can be used:

    //Illustrates a for-loop.
    #include <iostream>
    using namespace std;

    int main( )
    {
        int sum = 0;

        for (int n = 1; n <= 10; n++)         //Note that the variable n is a local
            sum += n;                 //n is a local variable within the loop!

        cout << "The sum of the numbers 1 to 10 is "
                << sum << endl;
        return 0;
    }

  10. An example of a syntactically correct (but may not be the best coding in the sense of hard to read and understand) use of the for-loop:

    float sales, total;
    int day, oneWeek;
    for (day = 1, oneWeek = 7, total = 0.0; day < = oneWeek; day++, total += sales)
    {
        cout << "Enter the sales for day " << day << ": ";
        cin >> sales;
    }

  11. For example, the for-loop is used in the following code to sum the series 1/2 + 1/3 + 1/4 + ... + 1/10:

    double sum = 0.0;
    for (int n = 2; n <= 10; n++)
        sum += 1.0/double(n);

  12. The initialization part of a for-loop may be omitted if it has already been performed, or no initialization is needed. However the semicolon is still required to keep the 3 parts separated from each other. The following shows an example:

    int num = 10;
    for ( ; num > 0; num--)
        cout << num << endl;

  13. The update part may also be omitted if it is performed elsewhere within the body of the loop. The following for-loop works exactly as a while-loop:

    int num = 10;
    for ( ; num > 0; )
    {
        cout << num << endl;
        num--;
    }

  14. The following is a syntactically correct for-loop, which unfortunately never stops:

    for ( ; ; )
        cout << "The EverReady battery never stops!" << endl;

  15. One common mistake is to put a semicolon after the head of the for-loop:

    int num = 10;
    for ( ; num > 0; num--);     // Should not have a semicolon here
        cout << num << endl;

    The compiler interprets that as a for-loop without a body and will not issue an error or even a warning. What is the resulting output?

  16. There are meaningful bodyless for-loops. The following example shows such a nifty (but borderline bad style) for-loop:

    for (int n = 10; n > 0; cout << n--);

    This loop actually does exactly the same thing as the one shown above in items 12 and 13.