13. While Loops

The purpose of a while loop is to repeat one or more statements. The primary difference between while and do loops is that the statements in a while loop may never execute, while those in a do loop will always execute at least once. The while and for loops work similarly (see the For Loops section for an explanation).

General Form

The general form of a while loop is as follows:

while (test) {
    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 While Loop Works

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

  1. The test is performed.
  2. If the test evaluates to true:

    1. The statements in the loop body are executed.
    2. Go back to Step 1.
  3. If the test evaluates to false, execution of the loop ends then execution passes to the first executable statement following the while loop.

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

Example – Loop Body Statements May Never Execute

The example that follows provides a while loop where the test might be false the first time it is evaluated:

int current_value = 0;
char answer;

std::cout << "Do you want to see any multiples of 4 (y/n)? ";
std::cin >> answer;

while (answer == 'y' || answer == 'Y') {
    current_value += 4;
    std::cout << "Current value = " << current_value
              << std::endl;
    std::cout << "Display the next value (y/n)? ";
    std::cin >> answer;
}

The code is a variation of the “User Controlled Loop” example in the Do Loops section. In this case, the user chooses whether to see any multiples of four at all. If the user’s answer is no, the while loop’s test is false and the loop body statements do not execute. If the user’s response is yes, the first multiple of four is displayed and the user is asked whether the next value should be shown. Multiples of four are displayed as long as the user continues to answer yes.

Note that the user is asked to make a decision at two points: 1) prior to the loop, the user is asked whether any value should be displayed, and 2) at the end of the loop body, the user is asked whether the steps should repeat. Both responses are necessary for the loop to work correctly.

Example – Loop Body Statements Always Execute

It is also possible to use a while loop such that the loop body statements will execute without question. An example is shown below:

int counter = 0;

while (counter < 3) {
    std::cout << "Hello" << std::endl;
    counter++;
}

The code is a variation of the “Counter Controlled Loop” example in the Do Loops section, which displays the word “Hello” three times. This example demonstrates a counter-controlled while loop.

Since the counter is initialized such that the test evaluates to true the first time it is evaluated, the code in the loop body will always execute.

Note that we cannot trust the loop to work correctly if counter is not initialized. Without this initialization, the variable’s value is garbage, so the test could evaluate to true or false (we could not know). When using a while loop this way, be sure to double check that the counter is initialized correctly.