The break Statement: Exiting a Loop Early
The continue statement lets the loop skip the rest of that one iteration and move on to the next one.
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 Selective Skip
A loop does not always need to process every item in the same way. Sometimes an iteration contains data that should be ignored, such as a comment line, an empty line, or input that fails validation. The continue statement lets the loop skip the rest of that one iteration and move on to the next one. The loop keeps running; only the current iteration is cut short.
The Control-Flow Jump
When execution reaches continue inside a loop, every statement still remaining in the current loop body is bypassed. Control then returns to the loop condition for a while loop or proceeds to the next iteration of a for loop. For a while loop, the condition is evaluated again. If it remains true, the loop body starts again from the beginning. This is why continue does not terminate the loop: it changes what happens in the current iteration, not whether later iterations can occur.
A Comment-Skipping Loop
Suppose a program reads lines until the user enters done. Lines beginning with a hash symbol represent comments. The program should ignore those lines but continue accepting other input. The continue statement is placed immediately after the comment check, before the main processing statement.
- The loop reads another line.
- The stopping input exits the loop.
- A comment line skips the remaining statements in this iteration.
- Only non-comment, non-stopping lines reach the print statement.
Tracing three kinds of input
Trace the loop for the inputs # note, hello, and done.
- 1# note
The line begins with a hash symbol, so continue runs. The print statement is skipped, and the loop reads the next line.
- 2hello
The line is not a comment and is not done. continue does not run, so the print statement processes the line.
- 3done
The stopping input causes break to exit the loop rather than continuing to another iteration.
Only hello is printed. The comment is skipped, and done ends the loop.
Skip or Stop
| Statement | Effect on current iteration | Effect on loop |
|---|---|---|
| continue | Skips the remaining statements | Keeps the loop running |
| break | Stops the current processing path | Exits the loop entirely |
Both statements alter normal loop flow, but only break terminates the loop.
Filtering Unwanted Data
A common use of continue is to reject unwanted data early in an iteration. The condition can identify an empty line, a comment, or data that fails validation. Once the unwanted case is found, continue skips the main processing logic. Valid cases reach that logic normally. This arrangement can be clearer than placing all of the main work inside a large conditional block, because the skip condition is handled near the beginning of the iteration.
for value in values:
if value < 0:
continue
print(value)- 1The loop examines each value in order.
- 2A negative value satisfies the skip condition.
- 3continue bypasses print for that iteration.
- 4Non-negative values reach print and are displayed.
5
10
0Mistakes to Avoid
Common mistake Treating continue as if it ends the loop
What it looks likeUsing continue when later input should still be examined
Why it failscontinue skips only the remainder of the current iteration. The loop continues with later iterations.
Do this insteadUse break when the entire loop must end.
Common mistake Expecting statements after continue to run
What it looks likePlacing a print or processing statement after continue in the same loop body
Why it failsAll remaining statements in that iteration are bypassed.
Do this insteadPlace code that should run for the current item before continue, or allow execution to reach it only when the skip condition is false.
Common mistake Hiding the main processing inside unnecessary deep nesting
What it looks likeWrapping a large body in a conditional solely to avoid unwanted items
Why it failsThe loop logic becomes harder to follow.
Do this insteadCheck the unwanted case early and use continue to make the skip explicit.
Trace the Inputs
Imagine a loop that reads the inputs 5, -3, 10, -1, 0, and quit. It uses continue whenever a number is negative and stops when the input is quit. Which numbers should be printed?
Hints
- A negative number causes the current iteration to be skipped.
- A skipped iteration does not print its number.
- The stopping input ends the loop rather than being printed.
Expected trace
Determine the output for 5, -3, 10, -1, 0, quit when negative values are skipped.
- 15
It is not negative, so it is printed.
- 2-3
continue skips this iteration, so it is not printed.
- 310
It is not negative, so it is printed.
- 4-1
continue skips this iteration, so it is not printed.
- 50
It is not negative, so it is printed.
- 6quit
The loop stops at the stopping input.
The printed numbers are 5, 10, and 0.
Key Takeaways
- 1continue skips the remaining statements in the current loop iteration.
- 2After continue, control returns to the loop condition or proceeds to the next loop iteration.
- 3The loop continues running after continue; it does not exit entirely.
- 4Use continue to filter unwanted or invalid items early and keep loop logic readable.
- 5Use break when the loop itself must terminate.
At a glance
- continue interrupts only the current iteration.
- Statements after continue in that iteration are skipped.
- The loop condition is checked again or the next iteration begins.
- continue filters selected iterations, while break exits the loop entirely.