The break and continue Statements
An interactive input loop uses while True to run indefinitely and break to exit when the user provides a specific command.
A Loop That Waits
Many interactive programs need to keep asking for input until the user signals that the interaction is finished. The loop must repeat for an unknown number of iterations, so the program cannot rely on a fixed count. Python's while True pattern provides the repetition, while break provides the stopping point.
while True keeps entering the loop because its condition is always true. The loop stops only when execution reaches a break statement inside it.
Tracing Each Input
This generated code follows the interactive loop described in the source. The loop begins with while True. Each iteration asks for input and stores the response in line. If line is exactly done, break runs immediately. Otherwise, the program prints the entered text and returns to the top of the loop. After break exits the loop, the final print displays Done!.
Inputs: apple, banana, done
Trace the loop when the user enters apple, then banana, then done.
First iteration: The user enters apple. The condition line == done is false, so break is skipped. The program prints apple and returns to the top of the loop.
Second iteration: The user enters banana. The condition is again false, so the program prints banana and starts another iteration.
Third iteration: The user enters done. The condition is true, so break immediately exits the loop. The remaining code in that iteration is not executed.
After the loop: Execution continues with the statement after the loop, which prints Done!.
The loop prints apple, prints banana, stops when done is entered, and then prints Done!.
What do you think happens?
What happens when the user enters done as the first input?
Reveal answer
Answer: The loop exits immediately and the code after the loop runs.
The input is checked before the print statement. When the condition matches done, break immediately exits the loop, so the remaining code in that iteration is skipped.
How break Controls Exit
break is a loop-control statement. When Python encounters it, execution leaves the loop immediately, regardless of how many iterations might otherwise remain. Python does not execute the remaining statements in the current iteration. Execution resumes with the code after the loop.
| Statement | Effect in the loop | What happens next |
|---|---|---|
| break | Exits the loop immediately | Execution continues after the loop |
| continue | Skips the remaining code in the current iteration | The loop begins its next iteration |
Commands Through the Loop
An interactive input loop can be understood as a repeated command-handling sequence: prompt the user, receive input, check whether it is the stop command, and then either exit or process the input. A non-stop command completes the current iteration and leads back to the prompt. The stop command takes a different path and reaches break.
The important trace question is not only what value the user entered, but which branch that value selects. For apple or banana, the condition is not met, so the input is processed and the loop repeats. For done, the condition is met, so the loop exits before the normal processing statement runs.
Mistakes That Prevent Exit
Forgetting the break statement
while True remains true, so the loop continues asking for input instead of leaving the loop.
Fix:
Place break in the branch that handles the intended exit command.Using the wrong exit command
The condition is not met when the user enters a different command, so break is skipped.
Fix:
Make the user instruction and the condition use the same exit command.Misplacing break outside the condition
The loop exits before it can process ordinary input.
Fix:
Put break inside the condition that identifies the stop command.
When building an interactive loop, include the exit condition deliberately, test the exact command that should stop the loop, and tell the user how to stop it. These checks make the loop's control flow predictable.
Practice the Trace
Trace the generated input loop for the inputs red, blue, done. Write down what is printed during each iteration, identify the iteration where break runs, and state what is printed after the loop.
Hints
- For each input, first compare it with done.
- Only inputs that are not done reach the print statement inside the loop.
- After break, execution continues with the statement after the loop.
Practice solution
Trace the loop for red, blue, done.
red: red does not match done, so it is processed and printed.
blue: blue does not match done, so it is processed and printed.
done: done matches the exit condition, so break exits the loop before the input is printed.
After exit: The statement after the loop runs and prints Done!.
The output contains red, blue, and then Done!. The word done is not printed by the loop.
Control Flow Summary
- while True creates a loop whose condition is always true, so it continues until a control statement exits it.
- An interactive input loop repeatedly prompts, checks the input, and either processes the input or stops.
- break immediately exits the loop and skips any remaining code in the current iteration.
- continue skips the remaining code in the current iteration and begins the next iteration instead of exiting the loop.
- A reliable loop needs a correctly placed break, the intended exit command, and clear instructions for the user.
Key Takeaways
- while True intentionally creates an indefinitely repeating loop.
- break provides the explicit exit when a condition such as an input matching done is met.
- Inputs that do not match the exit command are processed before the loop repeats.
- break exits the loop, whereas continue skips the rest of one iteration and starts the next.
- Tracing the condition check for each input reveals exactly when the loop repeats and when it ends.