Concepts / Using continue Statements

Using continue Statements

When Python encounters a continue statement, it immediately stops executing the current iteration and returns to the top of the loop to begin the next iteration. Any code between the continue statement and the end of the loop block is skipped for that iteration. This is different from a break statement, which exits the loop entirely.

  • Programming

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.

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.

evaluatetruejumpfalsefinish iterationLoop iterationSkip conditioncontinueNext iterationRemaining statements
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

for number in [1, 2, 3, 4]: if number == 2: continue print(number)

nextnextnext1print2continue3print4print
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

checktruenext iterationfalseUser inputLength below 3continueLoop topSufficient length
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
skip current iterationexit entirelycontinuenext iterationLoop continuesbreakloop exitLoop ends
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

  • Treating continue as if it ended the entire loop

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

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

  • Executing statements after continue in the same iteration

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

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

  • Assuming continue runs on every iteration

    continue is reached only when the relevant condition is true.

    Fix: Evaluate the condition separately for every loop iteration.

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.

Checking Each Value

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

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

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

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

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

cat hello

Key Takeaways

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

Key Takeaways

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