The break and continue Statements
while True keeps entering the loop because its condition is always true. The loop stops only when execution reaches a break statement inside it.
Try it: while Loops, break and continue
How a while loop re-evaluates its condition before every pass, how break leaves the loop at once, how continue jumps back to the condition, how a sentinel value ends a while True loop, and why a loop whose condition never becomes False runs forever.
How it works
- Evaluate the condition. If it is False, skip the body and continue after the loop.
- If it is True, run the body from the top, then go back and evaluate the condition again.
- continue skips the rest of the body and goes straight back to the condition check.
- break leaves the loop immediately; the condition is not checked again (while True loops rely on it, e.g. stopping at a sentinel like 'done').
- If nothing in the body can make the condition False, the loop never ends; the lab's guard stops it after N iterations and says so.
Default run (21 steps): Ready: Countdown. A while loop checks its condition before every pass. … Finished after 5 iterations and 6 condition checks.
Simplified: The lab's own small Python interpreter runs a fixed, hand-written script (you choose the variant and the values, never the code); nothing is executed as real Python. Every script was run in CPython 3.12 with the same inputs and the lab reproduces its output and error messages. input() returns the lines you type in the list, one per call (the prompt is not printed), and raises EOFError when they run out, like a closed keyboard stream. The guard is the lab's, not Python's.
Loading the simulation…
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.
- 1First 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.
- 2Second iteration
The user enters banana. The condition is again false, so the program prints banana and starts another iteration.
- 3Third iteration
The user enters done. The condition is true, so break immediately exits the loop. The remaining code in that iteration is not executed.
- 4After 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
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 |
Both statements change normal loop execution, but only break exits the loop.
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
Common mistake Forgetting the break statement
What it looks likeThe loop checks for the stop command but never executes break.
Why it failswhile True remains true, so the loop continues asking for input instead of leaving the loop.
Do this insteadPlace break in the branch that handles the intended exit command.
Common mistake Using the wrong exit command
What it looks likeThe program checks for done, but the user is told to enter quit.
Why it failsThe condition is not met when the user enters a different command, so break is skipped.
Do this insteadMake the user instruction and the condition use the same exit command.
Common mistake Misplacing break outside the condition
What it looks likebreak is placed so that it runs on every iteration.
Why it failsThe loop exits before it can process ordinary input.
Do this insteadPut 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.
- 1red
red does not match done, so it is processed and printed.
- 2blue
blue does not match done, so it is processed and printed.
- 3done
done matches the exit condition, so break exits the loop before the input is printed.
- 4After 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
- 1while True creates a loop whose condition is always true, so it continues until a control statement exits it.
- 2An interactive input loop repeatedly prompts, checks the input, and either processes the input or stops.
- 3break immediately exits the loop and skips any remaining code in the current iteration.
- 4continue skips the remaining code in the current iteration and begins the next iteration instead of exiting the loop.
- 5A reliable loop needs a correctly placed break, the intended exit command, and clear instructions for the user.
At a glance
- 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.