You can almost read a while statement as if it were English. What this means is, “While n is greater than zero, continue displaying the value of n and then reducing the value of n by 1. When you get to zero, output the word ‘Blastoff!’”
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.
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.
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:
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 . 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!
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.
The following code is a program to print the even numbers from 0 to 20. The code contains blanks. What is the correct while statement condition and iteration needed in order for the code to run successfully.
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.