What Is Break Statement ?

The break statement is a loop control keyword used to terminate a loop prematurely. The moment the program encounters break, it exits the loop instantly, completely ignoring any remaining code inside the loop and any remaining iterations.

The break Statement with for Loop

A for loop iterates over a sequence (like a range, list, or string). Use break when you find your target item and want to stop searching further to save computer memory and time.

Syntax

for variable in sequence:
    # Code inside the for loop
    if condition:
        break  # Terminates the loop
    # Remaining code inside the for loop
# Code outside the loop

Code Example

Stop printing numbers from a range of 1 to 10 as soon as the number reaches 5.

for i in range(1, 11):
    if i == 5:
        break  # Exits the loop immediately when i becomes 5
    print("Number:", i)

print("Loop terminated successfully.")

The break Statement with while Loop

A while loop runs as long as a test condition is True. You can use break to exit a while loop prematurely based on an independent internal condition.

Syntax

while condition:
    # Code inside the while loop
    if internal_condition:
        break  # Terminates the loop
    # Remaining code inside the while loop
# Code outside the loop

Code Example

Print a countdown from 5 downwards, but stop the loop if the counter hits 3.

counter = 5

while counter > 0:
    if counter == 3:
        break
    print("Counter is:", counter)
    counter -= 1

print("Out of the while loop.")

The break Statement in Nested Loops

A nested loop means a loop inside another loop. When you use a break statement inside the inner loop, it only terminates the inner loop. The outer loop will continue to run normally.

Syntax

for outer_variable in outer_sequence:
    for inner_variable in inner_sequence:
        if condition:
            break  # This only breaks the inner loop
    # Outer loop code continues here

Code Example

for i in range(1, 4):  # Outer Loop
    print(f"--- Outer Loop Row {i} Start ---")
    for j in range(1, 4):  # Inner Loop
        if j == 2:
            break  # Exits the inner loop when j is 2
        print(f"i = {i}, j = {j}")

Break Statement With if-elif-else

In Python, a break statement cannot sit alone inside a loop without a condition; otherwise, the loop would always terminate on its very first turn. To make it useful, break is placed inside conditional blocks (if, elif, or else).
Targeted Exit : The loop runs normally until a specific condition inside the if or elif block becomes True.
Instant Termination : The moment that specific block triggers the break, Python stops the entire loop dead in its tracks.
Skipping the Rest : Any remaining code inside the loop for that round, and all future rounds, are completely skipped.

Syntax Structure

When you have multiple conditions to check using an if-elif-else chain inside a loop, you can choose exactly which condition should safely terminate the program.

Syntax

for item in sequence:
    if condition_A:
        # Code runs, loop continues
    elif condition_B:
        break  # Loop terminates instantly if condition_B is True
    else:
        # Code runs for all other cases

Code Example

# List of incoming ATM requests
transactions = ["deposit", "withdraw", "suspend", "deposit", "check_balance"]

print("ATM System Initialized. Processing transactions...")

for action in transactions:
    # Condition 1: Check for regular deposits
    if action == "deposit":
        print("-> Processing Deposit: Money added successfully.")
        
    # Condition 2: Check for regular withdrawals
    elif action == "withdraw":
        print("-> Processing Withdrawal: Cash dispensed.")
        
    # Condition 3: Critical Security Trigger (The Break Condition)
    elif action == "suspend":
        print("\n[ALERT] Security breach or card blocked! Terminating all operations.")
        break  # Exits the entire loop immediately
        
    # Condition 4: Fallback for unhandled or invalid requests
    else:
        print(f"-> Warning: '{action}' is an invalid request.")

# Control jumps directly here after the break statement executes
print("\nATM System Closed Safely.")