Skip to content

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.

Beginner 10 min read·Lesson 76 of 306 in Python Programming

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

Text
for item in items:
if condition(item):
break
next_statement
  • break is usually inside an if.
  • The loop ends immediately.
  • Execution continues after the loop.

Code examples

Stop at the first match

Python
for n in [3, 8, 11, 14, 20]:
if n % 7 == 0:
print("First multiple of 7:", n)
break
print("checked", n)
Output
checked 3
checked 8
checked 11
First multiple of 7: 14

A menu loop

Python
while True:
choice = input("Command (add/quit): ").strip().lower()
if choice == "quit":
break
print("You chose", choice)
print("Goodbye")
Input
add
add
quit
Output
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

Python
SECRET = "py2026"
for attempt in range(1, 4):
guess = input(f"Password (attempt {attempt}): ")
if guess == SECRET:
print("Access granted")
break
else:
print("Account locked")
grid = [[1, 4, 7], [2, 9, 5], [8, 3, 6]]
found_at = None
for r, row in enumerate(grid):
for c, value in enumerate(row):
if value == 9:
found_at = (r, c)
break
if found_at:
break
print("9 found at", found_at)
Input
hello
py2026
Output
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:

TextInput
hello
py2026
TextOutput
Password (attempt 1): Password (attempt 2): Access granted
9 found at (1, 1)

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

for row in grid:
for cell in row:
if cell == target:
break # outer loop continues

Prefer

Use a flag or a function with return.

break exits only the loop it is directly inside.

Code after break in the same block

Avoid

if found:
break
print("found")

Prefer

if found:
print("found")
break

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

  1. 1
    Ask for numbers until the user enters a negative number, then print how many positive numbers were entered.
  2. 2
    Find 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?
Practise this topic