Module 6 · Loops and Iteration — topic 7 of 12
break
break exits a loop immediately. Learn when to use it (search, menu loops, attempt limits), how it interacts with nested loops, and how to keep loops with break easy to follow.
Learning objectives
- Exit a loop early with break
- Use break in searches and while True loops
- Explain that break only exits the innermost loop
Definition
break exits a loop immediately. Learn when to use it (search, menu loops, attempt limits), how it interacts with nested loops, and how to keep loops with break easy to follow.
Introduction
break stops the loop it is in straight away. Any remaining iterations are skipped, and the program continues with the first statement after the loop.
Why it matters
Often you don’t need to finish a loop: once you have found the item you were searching for, or the user chose to quit, carrying on wastes time or causes wrong behaviour. break expresses “we’re done” directly.
Detailed explanation
break can appear anywhere in a loop body, usually inside an if.
In a for loop, break skips the remaining items. In a while loop, it exits even if the condition is still true.
while True: plus break is a standard pattern when the exit test belongs in the middle of the body, such as after reading input.
In nested loops, break exits only the innermost loop. To leave both, use a flag variable, move the loops into a function and return, or use the loop else clause.
After a search loop with break you often need to know whether the item was found. A variable set before the loop (found = None) or the loop else clause records this.
Too many break statements in one loop make it hard to see when the loop ends. If you find yourself writing three or more, consider moving the loop into a function that returns its result.
Syntax
- break is usually inside an if.
- The loop ends immediately.
- Execution continues after the loop.
Code examples
Stop at the first match
checked 3 checked 8 checked 11 First multiple of 7: 14
A menu loop
add add quit
Command (add/quit): You chose add Command (add/quit): You chose add Command (add/quit): Goodbye
More examples
Limited attempts and breaking out of nested loops
hello py2026
Password (attempt 1): Password (attempt 2): Access granted 9 found at (1, 1)
Step-by-step explanation
- The for loop allows three attempts; break ends it on a correct password.
- The else clause runs only if the loop finished without break, so it isn’t printed here.
- In the grid search, the inner break only leaves the inner loop.
- Checking found_at after the inner loop lets the outer loop break too.
Expected output
Running “Limited attempts and breaking out of nested loops” with the sample input shown prints exactly:
Practical applications
- Login systems stop asking once the password is correct.
- Search features stop scanning when they find a match.
- Command-line programs run until the user types quit.
Best practices
- Use break to stop as soon as the answer is known.
- Keep exit conditions visible, near the top of the loop body when possible.
- Wrap nested searches in a function and return the result.
Common mistakes
Expecting break to leave all loops
Avoid
Prefer
break exits only the loop it is directly inside.
Code after break in the same block
Avoid
Prefer
Statements after break in the same block never run.
Key takeaways
- break exits the current loop immediately.
- It only affects the innermost loop.
- while True with break is a common input-loop pattern.
Try it yourself
- 1Ask for numbers until the user enters a negative number, then print how many positive numbers were entered.
- 2Find the first word longer than 6 letters in a sentence and stop searching.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?