next up previous contents
Next: Control in Loops: break Up: Programming Previous: for loops and the   Contents


while loops

While the for loop provides an excellent way to process the elements of a sequence, there are times when it's necessary to do some repetitive computation which is not based on an array. In those cases, the while loop can be used. The basic syntax of the while loop is as follows:
             while expression:
                 statements
             else:
                 statements
When python encounters a while loop, it firsts tests the expression provided; if it is not true, and an else clause is present, the statements following the else are executed. With no else clause, if the expression is not true, then control transfers to the first statement after the while loop.

If the expression is true, then the statements following the while statement are executed; when they are completed, the expression is tested once again, and the process is repeated. As long as the expression is true, execution of the statements after the while while be repeated; if the expression is not true, the statements after the else, if present, are executed. It should be noted that, as with the for loop, the else clause is not used very often with while loops, although there are situations where it can be used effectively.

To illustrate the while loop, consider an iterative process to calculate the cube root of a number. It's not necessary to understand the underlying math (based on Newton's method); it suffices to understand that, starting from an initial guess, we can calculate a new, better guess through a simple computation. But instead of repeating the process a fixed number of times (which could easily be accommodated by a for loop), we want to continue refining our guess until our answer is reasonably close to the correct one. In the case of calculating the cube root, a convenient criteria is that the absolute difference between the number we're working with and our guess cubed is small, say 1.e-8. The following python program uses a while loop to iteratively perform the calculation:

>>> num = 7.
>>> oldguess = 0.
>>> guess = 3.
>>> while abs(num - guess**3) > 1.e-8:
...     oldguess = guess
...     guess = oldguess - (oldguess**3 - num) / (3 * oldguess**2)
...
>>> guess
1.91293118277
>>> guess**3
7.0

Notice that the variables num, guess, and oldguess were all assigned values with decimal points included, to insure that calculations done with them would use floating point arithmetic as opposed to integer arithmetic (Section 3.1).

A common practice in many languages is to assign a value to a variable by calling a function which returns a non-zero value when it is successful, and a value of zero when it fails. This assignment is then used as the expression of a while loop. In python, this practice is not permitted, for a number of reasons. First, most python functions communicate failure by throwing an exception, not by returning a zero or None value. Furthermore, since an assignment (=) can so easily be mistaken for a test for equality (==) or vice versa, this practice often leads to very difficult-to-track bugs in your programs. For this reason, programming of while loops is often different in python than in other languages. In particular, since assignments can't be used as expressions in while loops, many python programmers write while loops as so-called ``infinite'' loops, and determine when to stop executing the loop through programming statements inside the loop using the techniques described in the next section.


next up previous contents
Next: Control in Loops: break Up: Programming Previous: for loops and the   Contents
Phil Spector 2003-11-12