Recognizing and Handling Infinite Loops
The break statement exits a loop immediately, skipping any remaining code in the loop body.
When Repetition Needs a Decision Inside
Some loops do not know in advance exactly when they should stop. Instead, the program performs an action, examines what happened, and then decides whether to continue. Python's break statement is designed for this situation: when execution reaches break inside a loop, the loop ends immediately and the remaining code in that loop body is skipped.
What do you think happens?
A program repeatedly asks for input. What should happen after the user enters hello, world, and then done?
Reveal answer
Answer: The loop prints hello and world, then stops before printing done.
For hello and world, the stop condition is false, so the loop prints the input and repeats. When the input is done, break executes immediately, so the print of that input is skipped and the loop continues with the statement after the loop.
The Immediate Effect of Break
break does two things at the moment it executes. First, it exits the loop. Second, it skips every statement that would otherwise come later in the current loop body. Control then continues with the statement after the loop. This is different from merely skipping one statement: break ends the repetition itself.
Tracing the Done Signal
The source example asks the user for input repeatedly until the word done is entered. Each repetition prints a prompt and waits for input. The program then checks whether the input equals done. If it does not, the program prints the input and returns to the top of the loop. If it does, break executes before the input-printing statement.
hello, world, done
Trace the source program when the user enters hello, then world, then done.
First repetition: The loop starts, prints the prompt, and receives hello. The input is not done, so break does not execute. The program prints hello and returns to the top.
Second repetition: The prompt appears again and the user enters world. The input is still not done, so the program prints world and returns to the top.
Third repetition: The prompt appears again and the user enters done. The condition is true, so break executes. The program skips the statement that would print the input and exits the loop.
After the loop: The statement after the loop prints Done! once.
The ordinary input output is hello followed by world. The input done is not printed by the loop, and Done! is printed once after the loop.
While True and Internal Exit Checks
The while True pattern creates an intentional infinite loop. Its repetition condition is always true, so the loop needs a break statement inside its body to end. This pattern is useful when the program discovers the stopping information during the loop, such as after receiving input. The exit condition can be placed where that information becomes available rather than being forced to appear at the top.
Affirmative Stop Conditions
There are two ways to describe the same stopping behavior. A traditional loop condition can say to keep going while the input is not done. The while True pattern can instead say to stop when the input is done. Both approaches can perform the same task, but the second states the exit event directly.
| Style | What the condition expresses | What the reader must reason about |
|---|---|---|
| Negative continuation | Keep going while the input is not done | Reason about a negated condition |
| Affirmative stop | Stop when the input is done | Read the stopping event directly |
Prefer the while True and break pattern when the exit condition is discovered inside the loop or when saying stop when this happens makes the logic easier to read. When the exit condition is simple and known before the loop starts, a traditional loop condition is often clearer.
Mistakes That Keep Loops Running
Assuming break skips only the current statement.
break exits the loop immediately and skips the remaining code in that loop body.
Fix:
Treat the statement after the loop as the next destination after break executes.Using while True without a reachable stopping condition.
while True intentionally keeps the loop condition true, so break is needed to terminate the loop.
Fix:
Verify that the stop condition can occur under realistic conditions.Checking the stop condition only at the top when the required information appears later.
The exit decision depends on information discovered inside the loop body.
Fix:
Place the exit check after the input or other event that supplies the stopping information.Describing a stop condition as a difficult negative continuation condition.
Negation can make the reader reason through an extra logical step.
Fix:
When appropriate, express the condition affirmatively as stop when the input is done.
Practice the Control Flow
Trace the input sequence hello, done for the source-style input loop. For each input, identify whether break executes, whether the ordinary input-printing statement runs, whether the loop repeats, and what happens after the loop.
Hints
- For hello, compare the input with the stop word done.
- For done, remember that break skips the remaining statements in the loop body.
- The statement after the loop runs only after the loop has exited.
- To solve the trace, hello does not meet the stop condition, so it is printed and the loop repeats. done meets the stop condition, so break exits immediately and the input-printing statement is skipped. The statement after the loop then runs.
Key Takeaways
- break exits a loop immediately and skips the remaining statements in the current loop body.
- while True creates an intentional infinite loop that must reach break to terminate.
- An exit check can appear inside the loop body, wherever the needed information becomes available.
- Affirmative stop wording describes when to stop directly, instead of describing the negated condition for continuing.
- Use a traditional loop condition when the exit condition is simple and known before the loop starts.
- Always verify that a break statement can execute under realistic conditions.
Key Takeaways
- break ends the loop immediately and skips all remaining code in that loop body.
- The while True and break pattern is useful when the program discovers its stopping condition during the loop.
- Exit checks can be placed after input or another event inside the loop, rather than only at the top.
- Affirmative conditions such as stop when the input is done can be clearer than negative continuation conditions.
- A loop using while True must have a realistic path to break, or it will remain infinite.