8A. While-Loops

While-loops

A C++ while-loop is nearly identical to a Java while-loop. It has the form

  while(condition)
  {
    statements
  }
The statements are called the loop body.

The loop starts by testing whether condition is true (or nonzero). If it is, then the statements in the loop body are performed and the loop goes back to its top, testing the condition again. The loop keeps going until it reaches its top and finds that condition is false (or 0). At that point, the loop ends, and the statement that follows the loop (if any) is performed. For example,

  int n = 0;
  while(n < 4)
  {
    printf("%i\n", n);
    n++;
  }
writes
  0
  1
  2
  3


Example: computing 1 + 2 + … + n

Loops are useful for solving problems that have … in their descriptions, where the number of steps depends on the value of a variable or parameter

A common-sense way to think of a loop is to think how you would solve the problem by hand, and then write the loop to do that. In the case of 1 + 2 + … + n, you might keep a running sum, as in the following function definition. You also need to keep track of where you are in the process, so there are two variables.

  //sum(n) returns 1 + 2 + ... + n

  int sum(const int n)
  {
    int s = 0;       // the running sum
    int i = 0;       // how far we have gone
    while(i < n)
    {
      i = i + 1;
      s = s + i;
    }
    return s;
  }

Hand simulation of a loop

Does the definition of sum work? Let's do a hand simulation of it with n = 3. Variables i and sum change each time the loop body is performed, which makes crossing out and replacing values messy. Let's instead show the values of i and sum in columns, with the most recent value at the bottom of the column. First, do the initializations.

     i    s    n
     0    0    3
Since the test i < n is true, we need to perform the loop body. First it adds 1 to i, giving
     i    s    n
     0    0    3
     1     
Next, it adds i to s. Notice that i is now 1.
     i    s    n
     0    0    3
     1    1
Again, i < n, so the loop body is done again. After doing the entire loop body, we have the following.
     i    s    n
     0    0    3
     1    1
     2    3
Since i is still less than n, we do the loop body again.
     i    s    n
     0    0    3
     1    1
     2    3
     3    6
Now i is not less than n. The loop is finished. Continuing with the next statement, the function returns 6, which is correct since 1 + 2 + 3 = 6.



Summary

Use a while-loop for repetition.

The next page offers advice on writing loops in a way that makes efficient use of your time (and do not involve hours of debugging).


Exercises

  1. Suppose that you have some statements STEP that you want to perform n times. Using a while-loop, write C++ statements to do that. Just write STEP to stand for whatever you want to repeat. Answer

  2. If n is nonnegative integer and x is a nonzero integer then you can compute xn by initializing p = 1 and then multiplying p by x a total of n times. Using a while-loop, write a definition of function power(x, n), which returns xn, where n and x are both integers, assuming n ≥ 0 and x ≠ 0. Answer

  3. The following has a while-loop whose body is hidden from you. The body does not contain a break, return or goto statement.

      while (x != 12)
      {
        …
      }
      y = x;
    
    Suppose you know that the loop eventually stops. What value is stored into y? Answer