Concepts / The break Statement: Exiting a Loop Early

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.

  • Programming
Interactive lab

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

  1. Evaluate the condition. If it is False, skip the body and continue after the loop.
  2. If it is True, run the body from the top, then go back and evaluate the condition again.
  3. continue skips the rest of the body and goes straight back to the condition check.
  4. 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').
  5. 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.

Educational simulation

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.

Loop conditionevaluateLoop bodycurrent iterationcontinueskip remaining statementsNext iterationloop starts againLoop endcondition is falsetruefalsecontinue reachedjumprecheck
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
Notes on the code
  • 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.
Worked example

Tracing three kinds of input

Trace the loop for the inputs # note, hello, and done.

  1. 1
    # note

    The line begins with a hash symbol, so continue runs. The print statement is skipped, and the loop reads the next line.

  2. 2
    hello

    The line is not a comment and is not done. continue does not run, so the print statement processes the line.

  3. 3
    done

    The stopping input causes break to exit the loop rather than continuing to another iteration.

Result

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

Both statements alter normal loop flow, but only break terminates the loop.

continueskip one iterationNext iterationloop continuesbreakexit loopAfter looploop endedjumpexit
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.

Code walkthrough
for value in values:
    if value < 0:
        continue

    print(value)
  1. 1
    The loop examines each value in order.
  2. 2
    A negative value satisfies the skip condition.
  3. 3
    continue bypasses print for that iteration.
  4. 4
    Non-negative values reach print and are displayed.
Output
5
10
0
Current itemone iterationSkip conditionunwanted data?continuebypass processingNext iterationloop continuesMain processinghandle iteminspectyesnojumpfinish body
How does a condition cause some iterations to skip their remaining processing while other iterations continue normally?

Mistakes to Avoid

  • Common mistake

    Treating continue as if it ends the loop

    What it looks like

    Using continue when later input should still be examined

    Why it fails

    continue skips only the remainder of the current iteration. The loop continues with later iterations.

    Do this instead

    Use break when the entire loop must end.

  • Common mistake

    Expecting statements after continue to run

    What it looks like

    Placing a print or processing statement after continue in the same loop body

    Why it fails

    All remaining statements in that iteration are bypassed.

    Do this instead

    Place 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 like

    Wrapping a large body in a conditional solely to avoid unwanted items

    Why it fails

    The loop logic becomes harder to follow.

    Do this instead

    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.
Worked example

Expected trace

Determine the output for 5, -3, 10, -1, 0, quit when negative values are skipped.

  1. 1
    5

    It is not negative, so it is printed.

  2. 2
    -3

    continue skips this iteration, so it is not printed.

  3. 3
    10

    It is not negative, so it is printed.

  4. 4
    -1

    continue skips this iteration, so it is not printed.

  5. 5
    0

    It is not negative, so it is printed.

  6. 6
    quit

    The loop stops at the stopping input.

Result

The printed numbers are 5, 10, and 0.

Key Takeaways

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

Related concepts

More concepts starting with T