Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use break to leave the nearest loop; use continue to skip the rest of the current iteration and move to the next one. In nested loops, either statement affects only the innermost enclosing loop. Python’s loop else clause runs when the loop finishes without break.

What each statement does

Statement Effect What happens next
break Ends the nearest enclosing for or while loop. Execution continues at the first statement after that loop.
continue Skips the rest of the current iteration of the nearest enclosing loop. A for loop advances to its next item; a while loop checks its condition again.

A for loop processes items from an iterable, while a while loop repeats as long as its condition is true. Both statements change the flow of the loop body, but they are not interchangeable.

When to use break

Use break when the loop’s task is complete or there is no reason to keep searching or repeating. The statements remaining in the loop body are skipped, and execution continues after the loop.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
for item in items:
    if should_stop(item):
        break
    process(item)

print("Loop is over")

If should_stop(item) is true, Python does not call process(item) for that item and proceeds to print.

When to use continue

Use continue when the current item should be ignored but later iterations should still be considered. Any statements later in the loop body are skipped for that iteration only.

for item in items:
    if should_skip(item):
        continue
    process(item)

When should_skip(item) is true, Python moves to the next item without calling process for the current one. With a while loop, continue moves to the next condition check, so make sure the loop’s state can still change and the condition can eventually become false.

How they behave in nested loops

In nested loops, break and continue apply to the nearest enclosing loop, not automatically to an outer one.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
for group in groups:
    for item in group:
        if should_stop(item):
            break
    print("Next group")

Here, break ends the inner loop. The outer loop continues, so print runs and the next group is processed. Python’s break and continue statements do not provide labels for targeting an outer loop. They must also be syntactically inside a for or while loop; placing one inside a function or class defined in that loop does not make it control the surrounding loop.

How loop else works

A loop’s else clause runs when the loop finishes without executing break. For a for loop, that means the iterable was exhausted; for a while loop, its condition became false. The else belongs to the loop, not to an if inside the loop body.

for candidate in candidates:
    if matches(candidate):
        print("found", candidate)
        break
else:
    print("no match")

If a match is found, break skips the loop’s else. If the loop reaches its natural end without a match, the else runs. A continue does not by itself prevent the else from running: it skips an iteration, and the clause still runs if the loop eventually finishes without break. A return or raised exception also prevents the loop’s else suite from running.

Cleanup in a finally clause

If control leaves a loop through break or moves to another iteration through continue, a finally clause in the path is executed first. This lets cleanup run before the loop exits or advances.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose by the outcome you need

  • Stop the loop as soon as a condition is met: use break.
  • Ignore one item but keep processing later items: use continue.
  • Take an action only when a search completes without finding a match: use the loop’s else, and use break when the match is found.

For the formal definitions and further examples, see the Python 3.14.8 tutorial’s section on control flow, the language reference for compound statements, and the language reference for simple statements.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.