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:
- The word
whileis a keyword. - The statements to repeat should be placed within the braces indicating the loop body.
- The test is a Boolean expression or compound condition, as needed.
- The parentheses are required around the test.
- If the loop body consists of multiple statements, the braces are required. If the loop body is only a single statement, the braces are optional.
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:
- The test is performed.
If the test evaluates to
true:- The statements in the loop body are executed.
- Go back to Step 1.
- If the test evaluates to
false, execution of the loop ends then execution passes to the first executable statement following thewhileloop.
Given how a while loop works, here are a few additional points:
- If the test evaluates to
falsethe first time it is evaluated, the statements in the loop body will never execute. - The keyword
whiledoes not perform any action. It only indicates the beginning of the loop. - As long as the test evaluates to
true, the statements in the loop body will repeat. - The test is referred to as a pretest because it is first performed before the loop body statements execute.
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.