14. For Loops

The purpose of a for loop is to repeat one or more statements. A for loop is a counter-controlled loop. The major difference between for and do loops is the location of the test. A counter-controlled while loop is functionally equivalent to (i.e., works the same as) a for loop.

General Form

The general form of a for loop is as follows:

for (initialization; test; adjustment)
{
    loop-body
}

Some important points:

Do not put a semicolon after the closing parenthesis at the top of the loop. If a semicolon is placed there accidentally, the loop will not work correctly.

The statements in the loop body should be indented and left aligned to visually emphasize they are in the loop.

How a For Loop Works

The steps taken when a for loop executes are as follows:

  1. The loop’s initialization is executed, giving the loop counter its initial value.
  2. The test is performed.
  3. If the test evaluates to true:

    1. The statements in the loop body are executed.
    2. The loop’s adjustment is executed giving the loop counter its next value.
    3. Go back to Step 2.
  4. If the test evaluates to false, execution of the loop ends then execution passes to the first executable statement following the for loop.

Given how a for loop works, here are a few additional points:

Example – Counting By One

The example that follows provides a for loop version of the code used to display the word “Hello” three times. Step through the code following the steps above:

int counter;

for (counter = 0; counter < 3; counter++) {
    std::cout << "Hello" << std::endl;
}

Since the initialization part of the for loop assigns a value of zero to the counter, the declaration does not do so. It would not hurt to initialize the variable in its declaration, but the step is unnecessary.

The loop body consists of a single statement, so the braces are optional. If removed, the code still works as expected. The single statement loop body should still be indented.

Compare the sequence of steps in this example to those in the “Loop Body Statements Always Execute” example in the While Loops section. As can be seen, the steps are identical. A for loop works the same as a counter-controlled while loop.

The postincrement operator is used in the adjustment. Three acceptable alternatives are as follows:

++counter
counter = counter + 1
counter += 1

Any one of the four choices is fine. Note that no semicolon appears in any option.

Example – Counting by a Value Other Than One

A for loop does not require only adjusting the counter by one or initializing the counter to zero:

int counter;

for (counter = 4; counter < 40; counter += 4) {
    std::cout << "Current value = " << counter << std::endl;
}

The code displays multiples of four. In this version, however, the program controls how many values are displayed, not the user. As can be seen, the counter is initialized to four, not zero, and the counter is increased by four, not one.

The loop counter has two roles in this example. It is being used, as expected, to end the loop’s repetition when it should stop. The counter is also used in the cout statement to display the multiples of four. What is the largest value displayed?