The while statement

Computers are often used to automate repetitive tasks. Repeating tasks without making errors is something that computers do well and people do poorly.

Loop (repetition) process is also called iteration. Java provides language features that make it easier to write these methods.

Using a while statement, we can rewrite countdown:

  public static void countdown(int n) {
    while (n > 0) {
      System.out.println(n);
      n = n-1;
    }
    System.out.println("Blastoff!");
  }


You can almost read a while statement like English. What this means is, “While n is greater than zero, print the value of n and then reduce the value of n by 1. When you get to zero, print 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.
3. If the condition is true, execute the statements between the squiggly brackets, and then go back to step 1.

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

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.

In the case of countdown, we can prove that the loop terminates if n is positive. In other cases it is not so easy to tell:

  public static void sequence(int n) {
    while (n != 1) {

      System.out.println(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 prints 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! For more information, see http://en.wikipedia.org/wiki/Collatz_conjecture.