while True Loops and Infinite Iteration
The continue statement skips the rest of the current loop iteration and jumps back to the top of the loop to begin the next iteration.
Why Skip an Iteration
A loop that accepts user input may receive different kinds of lines. Some lines may be comments that should be ignored, while another input may signal that the loop should end. The continue statement handles the first situation: it skips the rest of the current iteration and returns to the top of the loop. The break statement handles the second: it exits the loop entirely.
The Jump Made by continue
The continue statement skips the remainder of the current loop iteration and immediately returns to the top of the loop to begin the next iteration.
The important detail is what continue skips. It does not merely skip one statement. It skips every remaining statement in the current loop body. Control then returns to the top of the loop, where the next iteration begins. In a while True loop, this means the loop continues accepting input unless another path, such as break, exits it.
Tracing a Comment Line
while True: line = input() if line[0] == '#': continue print(line)
What do you think happens?
Suppose the input line begins with '#'. Which action occurs for that iteration?
Reveal answer
Answer: The line is discarded from processing, and the loop returns to the top.
When line[0] == '#' is true, continue executes. It prevents print(line) from running and starts the next iteration instead. It does not exit the loop.
Filtering Input Lines
A filter loop examines each input line and decides whether the line should receive the remaining processing. In this pattern, a line whose first character is '#' is treated as a comment. continue discards that line from the rest of the loop body, while other lines can reach print(line). The loop continues accepting more input after either path unless a separate termination condition uses break.
Combining continue and break
| Input condition | Statement | Effect |
|---|---|---|
| The line is 'done' | break | Exit the loop entirely |
| The first character is '#' | continue | Skip the current iteration and accept more input |
| Neither condition is true | print(line) | Process the line through the remaining statement |
The distinct control-flow roles of break and continue
Infinite Iteration and Exit
The word True in while True means the loop keeps beginning new iterations unless control leaves the loop. continue does not provide that exit; it sends execution back to the top. In the input filter pattern, break provides the exit when the user types done, while continue keeps the loop running after a comment line.
Common Control-Flow Mistakes
Treating continue as if it exits the loop.
continue skips only the current iteration. The loop continues running.
Fix:
Use break when a condition should exit the loop entirely.Assuming statements after continue still run in the same iteration.
continue skips the remainder of the current iteration, so the later print statement is not executed for that line.
Fix:
Trace the path from continue directly back to the top of the loop.Placing continue outside a condition.
The rest of every iteration is skipped instead of only the iterations matching a specific condition.
Fix:
Place continue inside an if statement or another conditional block.Using continue for the termination input.
continue keeps the loop running and accepts another input.
Fix:
Use break for the input that signals the end of the loop.
Practice the Trace
Trace the generated filter program using these inputs in order: a line beginning with '#', an ordinary line, and done. For each input, identify whether the program executes continue, break, or print(line). Then identify which input causes the loop to exit.
Hints
- Check the termination condition before the comment condition.
- continue skips the remaining statements for its current iteration.
- break exits the loop entirely.
Tracing Three Inputs
Determine the control-flow action for a comment line, an ordinary line, and done.
Comment line: The line is not done, but its first character is '#'. The program executes continue, skips print(line), and returns to the top.
Ordinary line: The line is not done and does not begin with '#'. The program does not execute either control statement, so print(line) runs.
done: The termination condition is true, so break executes. The loop exits before the comment test and print statement are reached.
The comment line is skipped, the ordinary line is printed, and done ends the loop.
Key Takeaways
- continue skips the remainder of the current loop iteration and returns to the top.
- A conditional continue can filter unwanted input, such as lines beginning with '#'.
- break exits the loop entirely, while continue keeps the loop running.
- In a while True loop, a termination condition using break provides the path out of repeated iterations.
- Trace the conditions in order to determine whether an input triggers break, continue, or the remaining statements.