Indefinite Loops

Our averaging program is certainly functional, but it doesn't have the best user interface. It begins by asking the user how many numbers there are. For a handful of numbers this is OK, but what if I have a whole page of numbers to average? It might be a significant burden to go through and count them up.

It would be much nicer if the computer could take care of counting the numbers for us. Unfortunately, as you no doubt recall, the for loop is a definite loop, and that means the number of iterations is determined when the loop starts. We can't use a definite loop unless we know the number of iterations ahead of time, and we can't know how many iterations this loop needs until all of the numbers have been entered. We seem to be stuck.

The solution to this dilemma lies in another kind of loop, the indefinite or conditional loop. An indefinite loop keeps iterating until certain conditions are met. There is no guarantee ahead of time regarding how many times the loop will go around.

In Python, an indefinite loop is implemented using a while statement. Syntactically, the while is very simple.

while <condition>: <body>

Here condition is a Boolean expression, just like in if statements. The body is, as usual, a sequence of one or more statements.

The semantics of while is straightforward. The body of the loop executes repeatedly as long as the condition remains true. When the condition is false, the loop terminates. Figure 8.1 shows a flowchart for the while. Notice that the condition is always tested at the top of the loop, before the loop body is executed. This kind of structure is called a pre-test loop. If the loop condition is initially false, the loop body will not execute at all.

Here is an example of a simple while loop that counts from 0 to 10: i=0

This code will have the same output as if we had written a for loop like this:

Flow Chart Pretest Loop
Figure 8.1: Flowchart of a while loop.

for i in range(11): print i

Notice that the while version requires us to take care of initializing i before the loop and incrementing i at the bottom of the loop body. In the for loop, the loop variable is handled automatically.

The simplicity of the while statement makes it both powerful and dangerous. Because it is less rigid, it is more versatile; it can do more than just iterate through sequences. But it is also a common source of errors. Suppose we forget to increment i at the bottom of the loop body in the counting example.

What will the output from this program be? When Python gets to the loop, i will be 0, which is less than 10, so the loop body executes, printing a 0. Now control returns to the condition; i is still 0, so the loop body executes again, printing a 0. Now control returns to the condition; i is still 0, so the loop body executes again, printing a 0

You get the picture. This is an example of an infinite loop. Usually, infinite loops are a bad thing. Clearly this version of the program does nothing useful. That reminds me, did you hear about the computer scientist who died of exhaustion while washing his hair? The instructions on the bottle said: "Lather. Rinse. Repeat."

As a beginning programmer, it would surprising if you did not accidently write a few programs with infinite loops—it's a rite of passage for programmers. Even more experienced programmers have been known to do this from time to time. Usually, you can break out of a loop by pressing Ctrl -c (holding down the <Ctrl> key and pressing "c"). If your loop is really tight, this might not work, and you'll have to resort to more drastic means (such as <Ctrl>-<Alt>-<Delete> on a PC). If all else fails, there is always the trusty reset button on your computer. The best idea is to avoid writing infinite loops in the first place.

Was this article helpful?

+2 -1

Responses

  • Jukka-Pekk Jehkinen
    How to make an definite loop and indefinite loop in a flowchart?
    4 months ago
  • awet
    How to loop indefinetly on python?
    3 months ago
  • odo
    How to create an indefinite loop in python?
    2 months ago
  • Lawrence
    Can you us while loop for indefinite loops.?
    2 months ago
  • stella
    When using an indefinite loop setting a end python?
    1 month ago

Post a comment