break

Updated March 15, 2026 · Keywords
keyword control-flow loop

The break statement in Python immediately terminates the nearest enclosing loop. It is commonly used to exit a loop early when a certain condition is met, rather than waiting for the loop to complete naturally.

Syntax

while condition:
    # loop body
    if exit_condition:
        break  # exit the loop immediately

for item in iterable:
    # loop body
    if exit_condition:
        break  # exit the loop immediately

How It Works

When Python encounters a break statement inside a loop:

  1. The loop terminates immediately
  2. Execution continues with the code after the loop
  3. The else clause of a for loop (if present) is skipped
# Searching for the first even number
numbers = [1, 3, 5, 6, 7, 8, 9]

for n in numbers:
    if n % 2 == 0:
        print(f"Found first even: {n}")
        break
# Output: Found first even: 6

Common Use Cases

Early exit from a loop

# Find the first prime number in a range
def find_first_prime(start, end):
    for num in range(start, end + 1):
        if num > 1:
            for i in range(2, int(num**0.5) + 1):
                if num % i == 0:
                    break  # not prime, try next number
            else:
                return num  # found prime!
    return None

print(find_first_prime(10, 20))
# Output: 11

Processing until condition is met

# Process items until a sentinel value
data = [1, 2, 3, -1, 4, 5]

for item in data:
    if item < 0:
        break
    print(f"Processing: {item}")
# Output:
# Processing: 1
# Processing: 2
# Processing: 3

Exiting nested loops

break only exits the innermost loop. To exit multiple nested loops, you can use a flag:

found = False
for i in range(5):
    for j in range(5):
        if i * j > 10:
            print(f"Found at i={i}, j={j}")
            found = True
            break
    if found:
        break
# Output: Found at i=3, j=4

Using with else

A for loop’s else clause runs only if the loop completes without hitting break:

# Check if a number is prime
number = 17
for i in range(2, int(number**0.5) + 1):
    if number % i == 0:
        print(f"{number} is not prime (divisible by {i})")
        break
else:
    print(f"{number} is prime!")
# Output: 17 is prime!

break vs return

Inside a function, return exits the function entirely, while break only exits the loop:

def find_item(items, target):
    for item in items:
        if item == target:
            return item
    return None

# Using break instead requires a different pattern:
def find_item_break(items, target):
    result = None
    for item in items:
        if item == target:
            result = item
            break
    return result

Practical Example

# Search for a value and stop early
products = [
    {"name": "apple", "price": 1.50},
    {"name": "banana", "price": 0.75},
    {"name": "orange", "price": 2.00},
    {"name": "grape", "price": 3.00},
]

budget = 2.50

for product in products:
    if product["price"] > budget:
        print(f"{product['name']} is too expensive!")
        break
    print(f"Buying {product['name']} for ${product['price']}")
# Output:
# Buying apple for $1.5
# Buying banana for $0.75
# orange is too expensive!

See Also