Using break Statements
The break statement provides a different kind of exit: as soon as break executes, loop execution stops immediately.
The Immediate Exit
A loop normally continues its work until its normal stopping condition is reached. The break statement provides a different kind of exit: as soon as break executes, loop execution stops immediately. Control moves to the first line of code after the loop.
Tracing the Jump
To trace break, focus on the exact moment it executes. The remaining statements in the current loop body are not executed, and the loop does not begin another iteration. Execution resumes at the first line after the loop. The key tracing question is: where does execution jump when break is encountered?
while True:
s = input("Enter text: ")
if s == "quit":
break
print("You entered:", s)
print("Loop finished")- 1The loop begins and asks the user to enter text.
- 2For an input other than "quit", the condition s == "quit" is false, so the loop's print statement runs.
- 3When the input is "quit", the condition becomes true and break executes.
- 4The loop terminates immediately, and execution continues with
print("Loop finished").
- The loop repeatedly asks for text.
- When s equals "quit", break executes.
- The print statement inside the loop is skipped for the "quit" input.
- Control continues with the print statement after the loop.
What do you think happens?
Suppose the inputs are hello and then quit. Which text is printed from inside the loop, and does the loop ask for another input after quit?
Reveal answer
The inside-loop message is printed for hello only, then execution moves after the loop.
The quit input makes s == "quit" true. break executes before the inside-loop print statement, so that statement is skipped for quit and the loop ends immediately.
Why Early Exit Helps
break is useful when the condition that should end the loop cannot be expressed conveniently in the loop header. The source identifies searching, user input, and error detection as situations where an early exit is useful. A repeated input loop is a clear example: the loop can continue asking for text, while break gives it a straightforward exit when the user enters the specified word.
Loop else Behavior
A loop can have an else block attached to it. The important rule is that if break executes, the loop's else block is skipped. Therefore, a loop that ends through break does not follow the same path as a loop that completes normally without break.
Tracing two possible endings
A loop has an else block. What determines whether that else block runs?
- 1Path 1
If the loop finishes normally and break never executes, control can proceed to the loop's else block.
- 2Path 2
If break executes during the loop, loop execution stops immediately and the loop's else block is skipped.
- 3Trace rule
When tracing, record whether break executed. That fact determines whether the loop else block is reached.
Normal completion allows the loop else block to run; break prevents it from running.
Nested Loop Scope
break only exits the innermost loop that directly contains it. If that loop is inside another loop, the outer loop continues normally. When tracing nested loops, first identify which loop contains the break statement directly; that is the loop that ends.
Common Tracing Mistakes
Common mistake Treating break as if it only skips the current statement.
What it looks likeAfter break executes, expecting the loop to continue with another iteration.
Why it failsbreak stops loop execution immediately, not merely one statement.
Do this insteadMove control directly to the first line after the loop in your trace.
Common mistake Expecting statements later in the same loop body to run after break.
What it looks likeAssuming the print statement after the break condition runs for the quit input.
Why it failsThe break exits before later loop-body statements can execute.
Do this insteadMark the remaining part of that iteration as skipped.
Common mistake Expecting a loop else block to run after break.
What it looks likeAssuming the else block is always reached when the loop ends.
Why it failsThe loop else block is skipped when break executes.
Do this insteadRecord whether the loop ended normally or through break.
Common mistake Assuming break exits every surrounding loop.
What it looks likePlacing break in an inner loop and expecting the outer loop to stop too.
Why it failsbreak only exits the innermost loop it is directly inside.
Do this insteadIdentify the directly enclosing loop before tracing the outer loop.
Trace It Yourself
Consider a repeated input process that continues asking for text until the user enters the specified quit word. Trace these inputs in order: first ordinary text, then another ordinary text, then quit. For each input, decide whether the inside-loop print statement runs, whether another iteration begins, and where execution goes after quit.
Hints
- The quit test becomes true only for the quit input.
- break skips the remaining loop-body statements for that iteration.
- After break, execution moves to the first line after the loop.
- 1Locate the break statement and identify the loop directly containing it.
- 2Trace each iteration until the exit condition becomes true.
- 3When break executes, stop tracing the loop immediately.
- 4Skip the remaining loop-body statements and all later iterations.
- 5Check whether a loop else block exists; if break executed, it is skipped.
- 6Continue at the first line after the loop.
Key Takeaways
- 1break stops the current loop immediately.
- 2Control jumps to the first line after the loop, skipping remaining loop work and later iterations.
- 3break is useful for early exits in searching, user-input processing, and error detection.
- 4A loop else block is skipped when break executes.
- 5In nested loops, break exits only the innermost loop that directly contains it.
At a glance
- break provides an immediate exit from a loop.
- After break executes, control continues at the first line after the loop.
- The remaining statements in the current iteration and all later iterations are skipped.
- A loop else block does not run when break exits the loop.
- Only the innermost directly containing loop is exited.