Skip to main content

Section 8.1 The while Statement

Looping or iteration is when a program repeatedly runs the same lines of code. A loop is created when the programmer specifies that the program need to jump back to a previous instruction instead of continuing to the next instruction.
The most fundamental loop structure in C++ is a while statement. It says β€œWhile the condition is true, do the body. Once the condition is false, continue with the code after the body.” Here is a simple example to watch in Codelens:
Listing 8.1.1.
Reading the code in English sounds like this: β€œSet with n set to 3. While n is greater than 0, print the value of n, and reduce the value of n by 1. After n is 0, print Blastoff!”
The flow of execution for a while statement is as follows:
  1. Evaluate the test (condition) in parentheses, which produces true or false.
  2. If the condition is false, skip the body (statements in braces).
  3. If the condition is true, execute the body and go back to step 1.
Flowchart showing the structure of a while loop.
Figure 8.1.2. Flow of execution for a while loop.
The test of the loop almost always has a variable as part of the expression it tests. Without a variable, there is no way for the loop to terminate once it starts. This variable is referred to as a loop control variable. The body of the loop must change the value of that variable so that, eventually, the condition becomes false and the loop terminates. Otherwise, the loop will repeat forever, which is called an infinite loop.
This example has an infinite loop. It will print the number 3 forever, or at least until you get bored of pressing Next.
Listing 8.1.3.

Warning 8.1.1.

When you make an infinite loop on an ActiveCode problem, it will display Compiling and running your program for ~10 seconds until the server decides to stop your program. If you run an infinite loop on your own computer, you will need to stop the program manually. You can generally do this by pressing Ctrl+C in the terminal window where the program is running. That sends a β€œkill” signal to the program, which should stop it.
It is also possible for the body of a loop to never end up executing. Notice in this sample that the condition is false the first time we reach it. That means the body will never get a chance to execute:
Listing 8.1.4.

Checkpoint 8.1.1.

The following code contains an infinite loop. Which is the best explanation for why the loop does not terminate?
int n = 10;
int answer = 1;
while (n > 0) {
    answer = answer + n;
    n = n + 1;
}
cout << answer;
  • n starts at 10 and is incremented by 1 each time through the loop, so it will always be positive.
  • The loop will run as long as n is positive. In this case, we can see that n will never become non-positive as the while statement condition will never be met.
  • The answer starts at 1 and is incremented by n each time, so it will always be positive.
  • While it is true that answer will always be positive, answer is not considered in the loop condition.
  • You cannot compare n to 0 in while loop. You must compare it to another variable.
  • It is perfectly valid to compare n to 0. Though indirectly, this is what causes the infinite loop.
  • In the while loop body, we must set n to False, and this code does not do that.
  • The loop condition must become False for the loop to terminate, but n by itself is not the condition in this case.

Checkpoint 8.1.2.

What is printed by this code? Make a memory diagram and trace the loop by hand.
int n = 1;
int x = 2;
while (n < 5) {
    n = n + 1;
    x = x + 1;
    n = n + 2;
    x = x + n;
}
cout << n;
cout << x;
  • Setting a variable so the loop condition would be false in the middle of the loop body does not keep the variable from actually being set.
  • Setting a variable so the loop condition would be false in the middle of the loop body does not stop execution of statements in the rest of the loop body.
  • After n becomes 5 and the test would be False, but the test does not actually come until after the end of the loop - only then stopping execution of the repetition of the loop.
You have attempted of activities on this page.