Coming soon after a section called "Avoiding Infinite Loops," you might be more than a bit surprised to see a section about creating infinite loops. Aren't infinite loops always a mistake? Well, if a loop were truly infinite, that is, it could never end, then yes, it would be a logical error. But what I call intentional infinite loops are infinite loops with an exit condition built into the loop body. The best way to understand an intentional infinite loop is to see an example.
The Finicky Counter program counts from 1 to 10 using an intentional infinite loop. It's finicky because it doesn't like the number 5 and skips it. Figure 3.13 shows a run of the program.
Figure 3.13: The number 5 is skipped with a continue statement and the loop ends through a break statement.
Here's the code to the program:
# Finicky Counter # Demonstrates the break and continue statements # Michael Dawson - 1/3/03 count = 0 while True: count += 1 # end loop if count is greater than 10 if count > 10: break # skip 5 if count == 5: continue print count raw_input("\n\nPress the enter key to exit.")
You know that any value can be interpreted as true or false, but Python also has a direct way to represent these values. True represents true. False (drumroll) represents false. You can use True and False like any other value. You can use them in a condition or even assign them to a variable. I'll show you what I mean through an interactive session.
>>> if True: print "I'm true!" I'm true!
Because True is true, the if block executes and prints the string "I'm true!"
>>> game_over = True >>> if game_over: print "Sorry, your game is over." Sorry, your game is over.
Because game_over is equal to True, the if block executes and prints the string "Sorry, your game is over."
>>> if False: print "I'm true!" else: print "I'm false!" I'm false!
Because False is not true, the if block is skipped and the else block runs, printing the string "I'm false!"
TRAP | True and False didn't exist in Python before version 2.2. In earlier versions of Python, it was common to use 1 to represent true and 0 to represent false. |
I set up the loop with:
while True:
This technically means that the loop will continue forever, unless there is an exit condition in the loop body. Luckily, I put one in:
# end loop if count greater than 10 if count > 10: break
Since count is increased by 1 each time the loop body begins, it will eventually reach 11. When it does, the break statement, which means "break out of the loop", is executed and the loop ends.
Just before count is printed, I included the lines:
# skip 5 if count == 5: continue
The continue statement means "jump back to the top of the loop." At the top of the loop, the while condition is tested and the loop is entered again if it's true. So when count is equal to 5, the program does not get to the print count statement.
Instead it goes right back to the top of the loop and 5 is skipped and never printed.
You can use break and continue in any loop you create. They aren't just restricted for use in intentional infinite loops. But they should be used sparingly. Both break and continue make it harder for someone (including you!) to see the flow of a loop and understand under what conditions it ends. Plus, you don't actually need break and continue. Any loop you can write using them can be written without them.
In Python, there are times when an intentional infinite loop can be clearer than a traditional loop. In those few cases, where it's really clunky to write the loop with a regular condition, some programmers use intentional infinite loops. But again, I say avoid them when possible.