Concepts / Loop Control Flow

Loop Control Flow

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 Skip

A loop does not always need to process every iteration in the same way. Sometimes one value should be skipped while the loop continues handling later values. Python provides continue for this situation. When Python reaches continue, it stops executing the current iteration and returns to the top of the loop to begin the next iteration.

The Continue Jump

A continue statement divides the current loop iteration into two possible paths. If execution reaches continue, Python immediately stops the current iteration. Every statement after continue and before the end of the loop block is skipped for that iteration. Execution then returns to the top of the loop, where the next iteration begins. If the condition that leads to continue is not met, execution proceeds through the remaining statements in the loop block normally.

checktruejump to topfalsefinish iterationLoop iterationSkip conditioncontinueNext iterationRemaining statementscurrent iteration
When Python reaches continue, which part of the current iteration is skipped and where does execution go next?

Statements after continue in the same loop block do not run for the iteration that reached continue. They can run during later iterations if those iterations do not reach continue.

Tracing an Iteration

Tracing the input sequence

For the input sequence hi, hello, x, and stop, determine what happens when the loop skips inputs shorter than three characters.

hi: The input has fewer than three characters, so continue is reached. Processing after continue is skipped, and the loop returns to the top for the next input.

hello: The input meets the minimum length requirement, so continue is not reached. The processing statement runs before the loop begins its next iteration.

x: The input has fewer than three characters, so continue is reached. Processing after continue is skipped, and the loop moves to the next iteration.

stop: This input is part of the stated sequence. The continue rule concerns the minimum length, so this input meets that requirement and the processing statement runs.

The short inputs hi and x are skipped by continue. The processing statement runs for hello and stop.

next iterationnext iterationnext iterationhicontinuehelloprocessxcontinuestopprocess
For each input, does execution skip the remaining body or reach the processing statement?

What do you think happens?

Which inputs reach the processing statement: hi, hello, x, or stop?

  • hi and x
  • hello and stop
  • all four inputs
  • only stop
Reveal answer

Answer: hello and stop

hi and x are shorter than three characters, so continue skips the processing statement for those iterations. hello and stop meet the minimum length requirement, so their processing statements run.

Filtering Without Exiting

A common use of continue is filtering. The loop can inspect each input, skip inputs that do not meet a minimum requirement, and continue processing later inputs. In the source example, strings shorter than three characters are not processed. The loop does not end after encountering a short string; it asks for or handles the next iteration instead.

inspecttruenext iterationfalsenext iterationInputLength below 3continueskip processingNext inputProcess input
How can a loop skip selected values while continuing to process later values?

Place continue at the point where the loop can clearly decide that the current item should be skipped. Then keep the processing statements after it. This makes the skip path and the process path easier to trace.

Continue Compared with Break

Continue and break both alter loop execution, but they do not have the same scope. Continue stops only the current iteration and returns to the top of the loop. Break exits the loop entirely. Use continue when later iterations should still be considered. Use break when the loop itself should end.

skips remainderexits loopcontinuecurrent iterationNext iterationbreakentire loopAfter loop
How does continue affect one iteration while break exits the entire loop?
StatementImmediate effectWhat happens afterward
continueStops the current iterationExecution returns to the top of the loop for the next iteration
breakExits the loop entirelyThe loop does not continue with later iterations

Mistakes in Control Tracing

  • Treating continue as if it ends the loop

    Continue skips only the rest of the current iteration. Execution returns to the top of the loop for the next iteration.

    Fix: Trace the next iteration after every continue before deciding whether the loop has ended.

  • Counting statements after continue as executed for that iteration

    Statements between continue and the end of the loop block are skipped during that iteration.

    Fix: Mark the current iteration as skipped from the continue point onward.

  • Using the effect of break when explaining continue

    Break exits the loop entirely, while continue returns to the top of the loop.

    Fix: Ask whether later iterations should still run. If they should, the described behavior is continue, not break.

Practice the Trace

MEDIUM

Trace the source input sequence hi, hello, x, and stop. For each input, write whether continue is reached, whether the processing statement runs, and which input is handled next.

Hints
  • Compare each input with the minimum length of three characters.
  • If continue is reached, skip the processing statement for that iteration.
  • After a continue, trace the next input rather than ending the loop.
next iterationnext iterationnext iterationhiomittedhelloprintedxomittedstopprinted
Which values are omitted because continue is reached first, and which values reach the output statement?

Key Takeaways

  1. Continue stops the current loop iteration immediately.
  2. Statements after continue in the same loop block are skipped for that iteration.
  3. Execution returns to the top of the loop and begins the next iteration.
  4. Continue is useful for skipping selected inputs while still processing later inputs.
  5. Break exits the entire loop, so it has a different effect from continue.

Key Takeaways

  • Continue skips the rest of only the current iteration.
  • After continue, execution returns to the top of the loop for the next iteration.
  • Statements after continue do not execute during the iteration that reaches it.
  • Continue can filter out selected inputs without ending the loop.
  • Break exits the entire loop, unlike continue.