Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Const

break

break exits the nearest enclosing for or while loop immediately. It is a statement rather than a callable value.

How it works

break immediately exits the closest enclosing for or while loop. It can be used only within such a loop, and it also prevents that loop's optional else clause from running; cleanup in a crossed finally clause still executes.

Syntax

break

Key points

  • The statement consists solely of the break keyword.
  • It is permitted only inside a syntactically enclosing for or while loop.
  • In nested loops, it exits only the innermost loop containing it.
  • After a broken for loop, its control target keeps the value from the current iteration.

Common mistakes

  • A loop's else block does not run when break terminates that loop.
  • A finally block runs before control leaves the loop.
  • A break inside a function or class defined in the loop is not valid for the surrounding loop.

Example: Stop a loop when a condition is met

for item in ["ready", "stop", "later"]:
    if item == "stop":
        break
    print(item)

Prints items until the first matching value, then exits the loop.

When to use it

Early search termination

Stop scanning as soon as a matching item is found.

Conditional loop exit

End polling or retry logic when a success condition occurs.

Inspect the matched iteration

Leave an inner loop while retaining its current for target.

Interview questions

QWhat does break do in nested loops?

break exits the nearest enclosing for or while loop immediately. It does not exit multiple nested loops at once.

Follow-up: How can code exit two nested loops? Use a flag, return from a containing function, or raise and handle an exception; one break only targets the nearest loop.

QHow does break interact with a loop's else clause?

A loop's optional else clause is skipped when the loop ends through break. The clause can therefore indicate that the loop completed without being interrupted by break.

Follow-up: What other exits skip the loop else clause? A return or a raised exception also prevents that clause from running.

QWhat happens to a for loop's control target after break?

If a for loop ends through break, its iteration target retains the value from the iteration that was active when the statement ran. This allows code after the loop to inspect that value.

Follow-up: Does this apply to while loops? The documentation specifies this target-value behavior for for loops, not while loops.

  • continue
  • for
  • while
  • try
  • finally

Source: 7. Simple statements. Checked 2026-10-16.