Concepts / Nested Loops and Loop Control

Nested Loops and Loop Control

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

  • Programming

A Stop Condition Inside the Loop

A loop does not always need to decide whether to continue before it begins its next iteration. With break, the loop can begin, perform work, check a condition at a chosen point in its body, and exit immediately when that condition is met. The key behavior is simple: break exits the loop at once and skips any code that remains in the current loop body.

What do you think happens?

A repeated input process receives hello, then world, then done. When done is received, what happens next?

  • The program prints done and asks for another input.
  • The loop exits, skips printing done as ordinary input, and the program prints Done! once.
  • The program stops before checking the input.
  • The loop continues forever.
Reveal answer

Answer: The loop exits, skips printing done as ordinary input, and the program prints Done! once.

The condition checks whether the input equals done. When the condition is true, break executes immediately, so the remaining print(line) statement is skipped. Control continues after the loop, where Done! is printed.

Tracing Immediate Exit

beginchecknorepeatyesexit loopLoop startsInput lineline equals donePrint linePrint Done!break
Which statements run when the stop condition becomes true inside the loop body?

The important trace is the difference between a non-stop input and the stop input. For hello, the equality check is false, so break does not execute. The program prints hello and returns to the top of the loop. The same happens for world. For done, the equality check is true, so break executes. The loop skips the print(line) statement and continues with the statement after the loop.

InputConditionLoop-body resultNext control location
helloline is not donePrint helloTop of loop
worldline is not donePrint worldTop of loop
doneline is doneSkip print(line)After loop

State trace for the repeated-input example

The while True Pattern

while True creates an intentional infinite loop. The loop is designed to keep iterating until a break statement is reached. This pattern is useful when the program discovers its exit condition inside the loop rather than knowing that condition before the loop starts.

enterchecknot metrepeatmet: breakwhile TruerepeatLoop bodyStop conditionNext iterationAfter loop
How does an intentionally infinite loop continue until its internal stop condition redirects control?

Exit Checks Beyond the Top

A traditional loop condition controls whether the next iteration begins. By contrast, break allows the loop to begin first and then check for an exit at a chosen point in the body. That check can occur after input is received or after other loop work has taken place. When the condition becomes true, the remaining statements in that iteration are skipped and control moves outside the loop.

startthennot metfinish bodynext iterationmet: breakEnter loopReceive inputExit conditionRemaining statementsAfter loopLoop top
How does control flow change when the exit condition is checked after work has started?

This placement matters because break does not merely prevent a future iteration. It interrupts the current one. Any statements below break in the same loop body are skipped. This is why the done input is not printed by print(line) in the source example.

Nested Repetition

Nested loops place one loop inside another. The loop-control idea remains the same: break exits a loop immediately and skips the rest of that loop body. When reading nested loops, trace the execution one loop body at a time. Identify which loop body is currently running, locate the condition that can trigger break, and then follow control to the code after the loop that break exits.

enterrepeatcontinue inner workinner loop endsrepeat outer workOuter iterationInner loopInner iterationNext outer iteration
How does execution move between an outer loop and the repeated work inside it?

Affirmative Stop Conditions

StyleQuestion the condition asksReading direction
Negative keep-going conditionShould the loop continue while the input is not done?Continue until the opposite condition becomes true
Affirmative stop conditionHas the input become done?Stop directly when the exit condition becomes true

The while True and break pattern lets you state the stop condition affirmatively: stop when this happens. The alternative is to state a negative condition: keep going while the stop event has not happened. Both approaches can perform the same task, but the affirmative form can be clearer because the reader sees the stopping event directly. This advantage is especially useful when the stop condition is complex or when there are multiple possible exit points.

Use break when the exit condition is discovered inside the loop or when affirmative phrasing makes the logic easier to read. Prefer a traditional loop condition when the exit condition is simple and known before the loop starts.

Common Tracing Mistakes

  • Treating break as if it only skips the next iteration.

    break exits the loop immediately and skips the remaining code in the current loop body.

    Fix: Mark every statement below break as skipped, then continue tracing with the code after the loop.

  • Checking the stop condition only at the top of the loop.

    break allows the exit condition to be checked anywhere in the loop body.

    Fix: Trace the body in order and evaluate the exit condition at the point where it appears.

  • Reading a negative condition without translating its meaning.

    The two statements use opposite phrasing even though they can describe the same behavior.

    Fix: Restate the logic as an affirmative stop event when that makes the control flow clearer.

  • Assuming while True will eventually stop automatically.

    while True intentionally creates an infinite loop; break is what provides the exit.

    Fix: Verify that the stop condition can become true under realistic inputs.

Practice Trace

MEDIUM

Trace the source example with the input sequence hello, world, done. For each input, record whether the stop condition is true, whether break executes, whether the input is printed, and where control goes next.

Hints
  • For hello and world, compare each value with done.
  • When the comparison is false, the program prints the input and returns to the top.
  • When the comparison is true, break skips print(line) and control continues after the loop.
  1. hello does not satisfy the stop condition, so it is printed and the loop repeats.
  2. world does not satisfy the stop condition, so it is printed and the loop repeats.
  3. done satisfies the stop condition, so break executes immediately.
  4. The print(line) statement is skipped for done.
  5. Control continues after the loop, where Done! is printed once.

Key Takeaways

  • break exits a loop immediately and skips the remaining statements in that loop body.
  • while True creates an intentional infinite loop whose exit must be provided by break.
  • An exit condition controlled by break can appear anywhere in the loop body.
  • Affirmative stop conditions can be clearer than negative keep-going conditions.
  • Always verify that break can execute; otherwise an intentional infinite loop can become accidental.