Concepts / The break Statement: Exiting a Loop Early

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

  • Programming

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.

truefalsecontinue reachedjumprecheckLoop conditionevaluateLoop bodycurrent iterationcontinueskip remaining statementsNext iterationloop starts againLoop endcondition is false
What happens next when continue is reached, and how does control return to the loop condition before the next iteration?

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.

python

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

StatementEffect on current iterationEffect on loop
continueSkips the remaining statementsKeeps the loop running
breakStops the current processing pathExits the loop entirely
jumpexitcontinueskip one iterationNext iterationloop continuesbreakexit loopAfter looploop ended
What is the difference between skipping the rest of one iteration and exiting the entire 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)

inspectyesnojumpfinish bodyCurrent itemone iterationSkip conditionunwanted data?continuebypass processingNext iterationloop continuesMain processinghandle item
How does a condition cause some iterations to skip their remaining processing while other iterations continue normally?

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

EASY

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

  1. continue skips the remaining statements in the current loop iteration.
  2. After continue, control returns to the loop condition or proceeds to the next loop iteration.
  3. The loop continues running after continue; it does not exit entirely.
  4. Use continue to filter unwanted or invalid items early and keep loop logic readable.
  5. 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.