Concepts / Conditional Statements (if/else)

Conditional Statements (if/else)

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 Loop Can Skip Without Stopping

A loop does not always need to process every iteration in the same way. Sometimes an input should be ignored for the current iteration, while the loop itself should continue running. Python provides the continue statement for this situation. When Python reaches continue, it immediately stops executing the current iteration and returns to the top of the loop to begin the next iteration.

Continue skips the rest of the current loop iteration. It does not exit the loop entirely.

evaluatecondition is trueskip remaining bodycondition is falseiteration endsIteration startsConditioncontinueNext iterationRemaining loop body
When the loop reaches continue, which statements are skipped, where does control flow next, and what output is produced?

Tracing the Skipped Work

The important detail is the position of continue inside the loop body. Any code between continue and the end of that loop block is skipped for the current iteration. Control then returns to the top of the loop. The next iteration begins, rather than continuing downward through the remaining statements from the skipped iteration.

Checking input length

A loop accepts user input and should process only strings that are at least 3 characters long.

Input a: The input has fewer than 3 characters. The continue statement is executed, so the processing statement at the end of the loop is skipped.

Input 12: This input also has fewer than 3 characters. The continue statement again skips the processing statement and returns control to the top of the loop.

Input abc: This input is at least 3 characters long. The condition that leads to continue is false, so continue is not executed.

Processing: Because continue was not executed for abc, the loop reaches the final processing statement and prints the sufficient-length message.

Short inputs are skipped, while abc reaches the processing statement. The loop continues to ask for input after each iteration.

check lengthtrueask againfalseiteration endsUser inputLength below 3continueProcess inputTop of loop
How does the condition determine whether execution follows the continue branch or the remaining loop-body branch?

Continue Compared with Break

StatementEffect on current iterationEffect on loop
continueStops executing the current iteration immediatelyThe loop continues with its next iteration
breakStops executing the current iteration immediatelyThe loop exits entirely

The difference is what happens after the current iteration stops. Continue returns to the top of the loop, so later iterations can still occur. Break exits the loop entirely, so no further loop iterations occur. Choose continue when an individual item should be skipped but the loop should keep examining later items. Choose break when the loop should end.

return to loopleave loopcontinuebreakNext iterationLoop exits
How does execution proceed differently after continue versus break, and does the loop run another iteration or end immediately?

Using Continue as a Filter

A useful pattern is to place a condition before the main processing statement. If the input does not meet the minimum requirement, continue filters it out of the current iteration. If the input meets the requirement, execution moves past the condition and reaches the processing statement.

Trace each iteration in order. For a, identify the true length condition, the continue statement, and the skipped processing. Repeat the same reasoning for 12. For abc, identify that the condition is false and follow the remaining loop body to the sufficient-length message.

What do you think happens?

What happens when the input is abc in the minimum-length example?

  • The loop exits immediately
  • The processing statement is skipped
  • The processing statement runs
Reveal answer

Answer: The processing statement runs

The continue condition is based on input length below 3. For abc, that condition is false, so continue is not executed and the final processing statement is reached.

Mistakes in Loop Tracing

  • Assuming continue ends the loop

    Continue skips only the current iteration and returns to the top of the loop.

    Fix: Trace the next iteration after every continue.

  • Counting statements after continue as executed

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

    Fix: Stop tracing the current iteration as soon as continue is reached.

  • Confusing continue with break

    Break exits the loop entirely, while continue begins the next iteration.

    Fix: Ask whether the control flow returns to the loop or leaves it.

When tracing a loop with continue, record three facts for every iteration: the input or value being examined, whether the condition leading to continue is true, and whether the remaining loop-body statements execute.

Trace It Yourself

MEDIUM

For the input sequence hi, hello, x, and stop, trace what happens at each step. Identify which print statements execute, which iterations are skipped, and why.

Hints
  • For each input, first determine whether the condition leading to continue is true.
  • If continue executes, stop tracing that iteration immediately.
  • Do not assume that continue exits the loop; distinguish it from break.
  1. Continue immediately stops the current loop iteration.
  2. Statements after continue in the same loop body are skipped for that iteration.
  3. After continue, control returns to the top of the loop for the next iteration.
  4. Break exits the loop entirely, unlike continue.
  5. Continue can filter out inputs that do not meet a requirement while allowing later iterations to be processed.

Key Takeaways

  • Continue skips the remainder of one loop iteration and starts the next iteration.
  • A condition can use continue to filter inputs before the main processing statements.
  • Statements after continue do not execute during the iteration that reached continue.
  • Break ends the loop entirely, whereas continue allows later iterations to run.
  • To trace a loop correctly, stop the current iteration at continue and resume at the loop's top.