Concepts / Using continue Statements

Using continue Statements

continue skips the rest of the current iteration. It does not end the entire loop.

  • 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 Detour

A loop can encounter an item that should not be processed. Sometimes the correct response is not to stop the loop, but to skip that one item and keep going. Python's continue statement provides that selective detour: it ends the current iteration immediately and sends execution back to the top of the loop.

Key point

continue skips the rest of the current iteration. It does not end the entire loop.

The Jump Back to the Loop

When Python reaches continue, it immediately stops executing the current loop body. Every statement located after continue in that loop block is skipped for the current iteration. Execution then returns to the top of the loop, where the next iteration begins. If the condition leading to continue is false, Python does not jump away and continues executing the remaining statements in the current iteration.

Loop iterationSkip conditioncontinueNext iterationRemaining statementsevaluatetruejumpfalsefinish iteration
What happens next when the loop reaches a continue statement?

The diagram shows the two possible paths. A true skip condition reaches continue and bypasses the remaining loop-body statements. A false condition allows those statements to run before the loop proceeds.

Tracing a Filtered Loop

Code walkthrough
for number in [1, 2, 3, 4]:
    if number == 2:
        continue
    print(number)
  1. 1
    For number equal to 1, the condition is false, so 1 is printed.
  2. 2
    For number equal to 2, continue executes, so the print statement is skipped.
  3. 3
    For number equal to 3, the condition is false, so 3 is printed.
  4. 4
    For number equal to 4, the condition is false, so 4 is printed.
  • The loop begins an iteration with the next value.
  • When number is 2, continue skips the print statement for that iteration.
  • For the other values, the condition is false and print executes.
Output
1
3
4
1print2continue3print4printnextnextnext
Which iterations execute the output statement, and which iteration skips it?

What do you think happens?

What values are printed by the loop before you read the answer?

  • 1, 2, 3, 4

  • 1, 3, 4

  • 1, 2

  • No values

Reveal answer
Answer:

1, 3, 4

The iteration for 2 reaches continue, so the print statement after it is skipped. The loop continues with 3 and then 4.

The important tracing habit is to inspect each iteration separately. Ask whether the skip condition is true. If it is true, cross out every statement after continue in that iteration. Then move to the next loop value. Do not cross out the same statements for later iterations unless their conditions also trigger continue.

Filtering Inputs Without Stopping

A common use of continue is to skip input that does not meet a minimum requirement. The source example uses a loop that accepts user input and checks its length. If the input is shorter than three characters, the condition len(user_input) < 3 is true. continue then skips the processing statement and returns to the top of the loop. If the input is at least three characters long, continue is not executed and the program prints that the input is of sufficient length.

python
InputConditionEffect
alen(user_input) < 3 is truecontinue skips the final print statement
12len(user_input) < 3 is truecontinue skips the final print statement
abclen(user_input) < 3 is falseThe program prints that the input is of sufficient length

How the input length determines the loop path

User inputLength below 3continueLoop topSufficient lengthchecktruenext iterationfalse
How does the input-length condition exclude short strings while allowing sufficient input to be processed?

Continue Versus Break

StatementEffect on current iterationEffect on the loop
continueStops the current iteration and skips the remaining loop-body statementsThe loop proceeds to its next iteration
breakStops execution at the point where it is encounteredThe loop exits entirely

The essential control-flow difference

continuenext iterationLoop continuesbreakloop exitLoop endsskip current iterationexit entirely
How does continue affect only the current iteration while break stops the entire loop?

Both statements alter normal loop execution, but they answer different questions. Choose continue when the current iteration should be abandoned and later iterations should still be considered. Choose break when the loop should end completely. Confusing these effects can cause a loop to process too much or to stop earlier than intended.

Mistakes in Execution Traces

  • Common mistake

    Treating continue as if it ended the entire loop

    What it looks like

    A loop reaches continue for one value, and the trace stops there.

    Why it fails

    continue skips only the current iteration. The loop returns to the top and begins the next iteration.

    Do this instead

    After recording the skipped statements, continue tracing the later iterations.

  • Common mistake

    Executing statements after continue in the same iteration

    What it looks like

    A print statement below continue is included in the output for the value that triggered continue.

    Why it fails

    All code between continue and the end of the loop block is skipped for that iteration.

    Do this instead

    Mark those statements as skipped, then move directly to the next iteration.

  • Common mistake

    Assuming continue runs on every iteration

    What it looks like

    The trace skips the processing statement even when the condition leading to continue is false.

    Why it fails

    continue is reached only when the relevant condition is true.

    Do this instead

    Evaluate the condition separately for every loop iteration.

Before
if len(user_input) < 3:
    continue
print("Input is of sufficient length")

For a short input, the print statement is incorrectly treated as if it runs.

After
if len(user_input) < 3:
    continue
# The print statement runs only when the condition is false.

For a short input, continue skips the print statement and the next iteration begins.

Trace It Yourself

MEDIUM

Trace the loop one iteration at a time. Which values reach the print statement, and which value causes continue to run?

for value in ["a", "cat", "12", "hello"]:
    if len(value) < 3:
        continue
    print(value)
Hints
  • Evaluate len(value) < 3 for each value.
  • When the condition is true, skip the print statement for that iteration.
  • Keep tracing after a continue; it does not end the loop.
Worked example

Checking Each Value

Determine the output for the loop that processes a, cat, 12, and hello while skipping values shorter than three characters.

  1. 1
    a

    The length is below 3, so continue runs and the value is not printed.

  2. 2
    cat

    The length is not below 3, so continue does not run and cat is printed.

  3. 3
    12

    The length is below 3, so continue runs and the value is not printed.

  4. 4
    hello

    The length is not below 3, so continue does not run and hello is printed.

Result

cathello

Key Takeaways

  1. 1
    continue immediately ends the current loop iteration.
  2. 2
    Statements after continue in the same loop block are skipped for that iteration.
  3. 3
    After continue, execution returns to the top of the loop and begins the next iteration.
  4. 4
    continue is useful for filtering or skipping selected inputs without ending the loop.
  5. 5
    break exits the entire loop, while continue affects only the current iteration.

At a glance

  • continue skips the rest of the current iteration and starts the next one.
  • A condition can use continue to reject selected inputs while allowing the loop to process later inputs.
  • To trace a loop, evaluate the continue condition for each iteration and skip only the statements below continue in that iteration.
  • continue keeps the loop running, whereas break exits the loop entirely.

Related concepts

More concepts starting with U