Using break to Exit Loops Early
The continue statement skips the rest of the current loop iteration and jumps back to the loop start, enabling efficient filtering of uninteresting data.
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 Filter Inside the Loop
A loop may examine many input lines even though only some of them matter. The continue statement lets the loop discard an uninteresting line immediately. It stops the current iteration and sends control back to the top of the loop, where the next line can be examined.
Tracing the Skip Path
The loop first tests whether the current line starts with From:. The condition uses not, so a line that does not start with From: is identified as uninteresting. For that line, continue runs immediately. The print statement is below continue, so it is skipped for that iteration. A line that does start with From: does not enter the continue path and reaches print instead.
The Search Pattern
The pattern if not condition: continue is useful because it describes the lines to reject first. If the condition is true, the line is interesting and the code below can process it. If the condition is false, not condition is true, so continue skips the line. This keeps the processing code focused on matching lines rather than nesting it inside another conditional branch.
for line in lines:
if line.startswith("From:"):
print(line)Processing is nested inside the matching condition.
for line in lines:
if not line.startswith("From:"):
continue
print(line)Uninteresting lines leave the iteration early; matching lines reach the processing statement.
A Three-Line Trace
Use a small input to trace the loop rather than trying to reason about every line at once. The sample contains Subject: Meeting, From: alice@example.com, and Date: 2024-01-15. Only the second line begins with From:.
| Iteration | Input line | Condition result | Loop action | Reaches print? |
|---|---|---|---|---|
| 1 | Subject: Meeting | Does not start with From: | continue skips the rest of this iteration | No |
| 2 | From: alice@example.com | Starts with From: | Continue is not executed | Yes |
| 3 | Date: 2024-01-15 | Does not start with From: | continue skips the rest of this iteration | No |
Tracing the search loop one input line at a time
From: alice@example.comCompact Conditional Syntax
Python allows an if statement and its action to appear on one line. In this contracted form, the condition comes first, followed by a colon and the action. The one-line form functions identically to the indented multi-line form.
| Multi-line form | Contracted form |
|---|---|
| if not line.startswith("From:"): continue | if not line.startswith("From:"): continue |
| The action appears on the indented next line. | The action appears after the colon on the same line. |
Two equivalent ways to write the continue condition
Continue or Break
| Statement | Effect on the current iteration | Effect on the loop |
|---|---|---|
| continue | Stops executing the remaining statements | Jumps back to the loop start for the next iteration |
| break | Stops executing the current iteration | Exits the loop entirely |
The essential control-flow difference
Common mistake Treating continue as if it ended the entire loop.
What it looks likeA non-matching line executes continue, but later input lines are still examined.
Why it failscontinue skips only the rest of the current iteration and then returns control to the loop start.
Do this insteadUse continue when the loop should move to the next item. Use break when the loop should end immediately.
Common mistake Expecting code after continue to run for the same line.
What it looks likeThe print statement below continue is not reached for a line that fails the search condition.
Why it failsPython immediately stops executing the rest of that iteration after continue.
Do this insteadPlace code below continue only when it should process lines that pass the filter.
Common mistake Forgetting that not reverses the test.
What it looks likeif not line.startswith("From:"): continue skips lines that do not start with From:.Why it failsThe pattern tests for the lines to skip, not the lines to process.
Do this insteadRead the condition as: if this line is uninteresting, continue.
Try the Trace
Suppose a search loop uses if not line.startswith("From:"): continue, followed by a processing statement. Predict which of these lines reach the processing statement: Subject: Meeting, From: bob@example.com, and Date: 2024-01-15. Then explain what happens to the other two lines.
Hints
- The condition identifies lines that do not start with From:.
- For a line that matches that description, continue skips the remaining statements in the iteration.
- 1Read the current input line.
- 2Ask whether it starts with From:.
- 3If it does not, follow continue and move to the next iteration.
- 4If it does, let the line reach the processing statement.
- 5Repeat the trace for every input line.
Key Takeaways
- 1continue skips the rest of the current loop iteration and starts the next iteration.
- 2The pattern if not condition: continue filters out uninteresting items before the processing code.
- 3Only lines that avoid continue reach statements placed below it.
- 4continue differs from break because break exits the entire loop.
- 5A contracted if places the action on the same line as the condition and behaves like the equivalent multi-line form.
At a glance
- continue is an early skip for one loop iteration, not a command to end the whole loop.
- if not condition: continue makes a search loop reject uninteresting lines before processing matching lines.
- Tracing each input line reveals exactly which statements are skipped and which are reached.
- The contracted if form keeps a condition and its continue action on one line.
- break exits the loop entirely, so it has a different purpose from continue.