Concepts / Using continue to Skip to the Next Iteration

Using continue to Skip to the Next Iteration

The break statement exits a loop immediately, skipping any remaining code in the loop body.

  • Programming

The Stop Decision

The supplied material for this article focuses on break, not on the behavior of continue. Break exits a loop immediately when a specific condition is met. It also skips every remaining statement in the current loop body. The central question is this: when the program discovers that it should stop, how does control flow leave the loop?

Tracing an Immediate Exit

What do you think happens?

A loop reads hello, then world, then done. What happens when the input is done?

  • The program prints done and starts another iteration
  • The loop exits immediately and skips the print inside the loop
  • The loop ignores done and continues forever
Reveal answer

Answer: The loop exits immediately and skips the print inside the loop.

The stop condition is true when the input equals done, so break executes. The remaining loop-body statement is skipped, and control moves to the statement after the loop.

nonext iterationyesexit loopLoop startsRead inputInput is donePrint inputDone!break
When break executes, which statements and future iterations are skipped?

The Input Trace

Reading Until done

Trace the described program when the user enters hello, world, and then done.

First iteration: The loop starts and waits for input. The user enters hello. The condition asking whether the input equals done is false, so break does not execute. The program prints hello and returns to the top of the loop.

Second iteration: The program waits for input again. The user enters world. The condition is false again, so the program prints world and returns to the top.

Third iteration: The user enters done. This time the condition is true, so break executes. The loop exits immediately, and the print of the current input is skipped.

After the loop: Control continues with the statement after the loop. The program prints Done! once.

hello and world are printed from inside the loop. done is not printed by that loop-body statement. The loop ends, and Done! is printed afterward.

The important boundary is the moment break executes. It does not merely skip one output or postpone a decision. It exits the loop immediately, so both the rest of the current loop body and every later iteration are skipped.

Intentional Infinite Loops

The while True pattern creates an intentional infinite loop. Its repeated execution is not meant to continue forever in a finished program; break supplies the actual path out when a specific condition is met. This pattern is useful when the program must first perform work, such as obtaining input, before it can know whether it should stop.

each iterationnot metnext iterationmet: breakwhile TrueRead inputStop conditionLoop workAfter loop
How does repeated execution continue, and where does break provide the exit path?

Exit Conditions Inside the Body

A traditional loop condition is checked at the top of a loop. The break pattern allows the program to check its exit condition anywhere in the loop body instead. This matters when the program must obtain input or perform an earlier step before the stopping information exists.

not metrepeatmet: breakBegin iterationObtain inputStop conditionLater loop workNext iterationAfter loop
How does control flow change when the loop obtains information before checking whether it should stop?

In the input example, the condition belongs after the input operation because the program cannot compare the current input with done until that input has been received. When the condition is false, the remaining work runs. When it is true, break prevents that remaining work from running.

Affirmative Stop Conditions

Negative phrasingAffirmative phrasing
Keep going while the input is not doneStop when the input is done
The reader must interpret a negationThe stopping event is stated directly
The loop condition describes continuationThe break condition describes exit

The while True with break pattern lets you say when to stop rather than when to keep going. The affirmative form can be clearer because the reader sees the stopping event directly. The source material notes that this is especially valuable when the stop condition is complex or when there are multiple possible exit points.

Common Control-Flow Mistakes

  • Treating break as if it only skips the current statement.

    Break exits the loop immediately, so the remaining loop-body statements do not run.

    Fix: Trace the path from break to the statement after the loop.

  • Assuming the loop will naturally stop after break executes.

    Break skips every later iteration as well as the remainder of the current one.

    Fix: Mark the loop as finished as soon as break executes.

  • Creating a while True loop without a realistic break condition.

    The loop has no effective exit path and can become an accidental infinite loop.

    Fix: Check that realistic program input or state can make the break condition true.

  • Placing the exit test before the information needed for that test exists.

    A condition cannot inspect the current input until the program has obtained that input.

    Fix: Place the break check where the relevant information becomes available.

  • Using a negatively phrased condition when an affirmative stop condition would be clearer.

    The reader must mentally interpret the negation instead of seeing the stopping event directly.

    Fix: When appropriate, express the event that means stop and use break.

Control-Flow Practice

MEDIUM

Trace a loop that reads three inputs in this order: alpha, done, beta. Identify which inputs are printed inside the loop, when break executes, whether beta is processed, and what kind of statement can run after the loop.

Hints
  • The stop condition is checked after each input is received.
  • Break skips the rest of the current loop body and every later iteration.
  • Compare the input that triggers the stop with the inputs that do not.

The trace is alpha first, then done. Alpha does not satisfy the stop condition, so the ordinary loop work occurs. Done satisfies the condition, so break exits immediately. Beta is not processed because the loop has already ended. Control then moves to the statement after the loop.

Key Takeaways

  1. Break exits a loop immediately and skips the remaining statements in its current loop body.
  2. The while True pattern creates an intentional infinite loop whose actual exit is supplied by break.
  3. An exit condition can be checked inside the loop body, including after earlier work in the iteration.
  4. Affirmative phrasing states when to stop directly, while a negative loop condition states when to keep going.
  5. Every break-based loop should have a realistic path to its exit.

Key Takeaways

  • Break leaves a loop immediately and skips all remaining loop-body statements.
  • The while True pattern is useful when the program discovers its stopping condition inside the loop.
  • Checking an exit condition inside the body allows the condition to be placed where the relevant information becomes available.
  • Affirmative stop conditions can be clearer than negatively phrased keep-going conditions.
  • A break statement must be reachable under realistic conditions, or the loop may run indefinitely.