5.3. The while statement

Using a while statement, we can rewrite countdown:

Try running the active code below! Afterwards, try changing 9 to a different integer to see how the function works!

You can almost read a while statement as if it were English. What this means is,

“While n is greater than zero, display the value of n and then reduce the value of n by 1. When you get to zero, output the word ‘Blastoff!’”

More formally, the flow of execution for a while statement is as follows:

  1. Evaluate the condition in parentheses, yielding true or false.

  2. If the condition is false, exit the while statement and continue execution at the next statement following the while block.

  3. If the condition is true, execute each of the statements between the braces, and then go back to step 1.

The general form for a while loop is:

while (CONDITION) {
  BODY
}

This type of flow is called a loop because the third step loops back around to the top. Notice that if the condition is false the first time through the loop, the statements inside the loop are never executed. The statements inside the loop are called the body of the loop.

The body of the loop should change the value of one or more variables so that, eventually, the condition becomes false and the loop terminates. Otherwise the loop will repeat forever, which is called an infinite loop. An endless source of amusement for computer scientists is the observation that the directions on shampoo, “Lather, rinse, repeat,” are an infinite loop.

Warning

Make sure your while loops don’t loop forever! If they are meant to terminate, make sure to change the value of a variable, like incrementing or decrementing a counter.

New programmers often mistake unintended infinite loops for a program “hang” – the program appears to be doing nothing, yet not reposnding to user input. In fact, your program is very busy running your loop!

In the case of countdown, we can prove that the loop will terminate because we know that the value of n is finite, and we can see that the value of n gets smaller each time through the loop (each iteration), so eventually we have to get to zero. In other cases it is not so easy to tell:

void sequence (int n) {
  while (n != 1) {
    cout << n << '\n';
    if (n%2 == 0) {           // n is even
      n = n / 2;
    } else {                  // n is odd
      n = n*3 + 1;
    }
  }
}

The condition for this loop is n != 1, so the loop will continue until n is 1, which will make the condition false.

At each iteration, the program outputs the value of n and then checks whether it is even or odd. If it is even, the value of n is divided by two. If it is odd, the value is replaced by \(3n+1\). For example, if the starting value (the argument passed to sequence) is 3, the resulting sequence is 3, 10, 5, 16, 8, 4, 2, 1.

Since n sometimes increases and sometimes decreases, there is no obvious proof that n will ever reach 1, or that the program will terminate. For some particular values of n, we can prove termination. For example, if the starting value is a power of two, then the value of n will be even every time through the loop, until we get to 1. The previous example ends with such a sequence, starting with 16.

Particular values aside, the interesting question is whether we can prove that this program terminates for all values of n. So far, no one has been able to prove it or disprove it!

Sometimes you want an ironclad guarantee that a while loop body will execute at least once. In that case, C++ provides an alternate while statement, the do-while loop. The general form for a do-while loop is:

do {
  BODY
}
while (CONDITION);

Note

Notice the do-while loop ends in a semi-colon (;), while the first form does not.

This is a common ommission and is a compile error.


More to Explore

You have attempted of activities on this page