The break Statement: Exiting a Loop Early
continue skips the rest of the current loop iteration and jumps back to the loop condition to begin the next iteration
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.
Tracing three kinds of input
Trace the loop for the inputs # note, hello, and done.
# note: The line begins with a hash symbol, so continue runs. The print statement is skipped, and the loop reads the next line.
hello: The line is not a comment and is not done. continue does not run, so the print statement processes the line.
done: 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 |
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)
Mistakes to Avoid
Treating continue as if it ends the loop
continue skips only the remainder of the current iteration. The loop continues with later iterations.
Fix:
Use break when the entire loop must end.Expecting statements after continue to run
All remaining statements in that iteration are bypassed.
Fix:
Place code that should run for the current item before continue, or allow execution to reach it only when the skip condition is false.Hiding the main processing inside unnecessary deep nesting
The loop logic becomes harder to follow.
Fix:
Check 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.
5: It is not negative, so it is printed.
-3: continue skips this iteration, so it is not printed.
10: It is not negative, so it is printed.
-1: continue skips this iteration, so it is not printed.
0: It is not negative, so it is printed.
quit: The loop stops at the stopping input.
The printed numbers are 5, 10, and 0.
Key Takeaways
- continue skips the remaining statements in the current loop iteration.
- After continue, control returns to the loop condition or proceeds to the next loop iteration.
- The loop continues running after continue; it does not exit entirely.
- Use continue to filter unwanted or invalid items early and keep loop logic readable.
- Use break when the loop itself must terminate.
Key Takeaways
- 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.