Codectionary / Developer documentation / Python

break and continue

break and continue give you fine-grained control over loop execution. break immediately exits the nearest enclosing loop entirely, skipping any remaining iterations. continue skips just the rest of the current iteration and jumps straight to the next one, without exiting the loop. Both work inside for and while loops.

Syntax

break\ncontinue

Examples

Using break

Exiting a loop early once a condition is met.

numbers = [1, 3, 5, 8, 9, 12]

for num in numbers:
    if num % 2 == 0:
        print(f"Found first even number: {num}")
        break
    print(f"{num} is odd")

Using continue

Skipping specific iterations while letting the loop continue.

for num in range(1, 11):
    if num % 2 != 0:
        continue  # skip odd numbers
    print(num)  # only prints even numbers: 2, 4, 6, 8, 10

Combining break and continue

Using both together to filter and stop early within the same loop.

passwords = ["abc", "password123", "hunter2!", "12345", "SecureP@ss1"]

for pwd in passwords:
    if len(pwd) < 8:
        continue  # skip anything too short

    if pwd == "SecureP@ss1":
        print("Found the target password!")
        break

    print(f"Checked: {pwd}")

Best practices

  • Use break to exit a loop as soon as further iteration is pointless, avoiding unnecessary work
  • Use continue to skip invalid or irrelevant items without wrapping the rest of the loop body in an if block
  • In nested loops, remember break and continue only affect the innermost loop they are directly inside
  • Avoid overusing break/continue in ways that make loop logic hard to follow - sometimes restructuring the condition is clearer

At a glance

Purpose
Scripting and general-purpose applications
File extension
.py
Runs in
Python interpreter
Usually used with
Python standard library and packages

Specifications & further reading

Related Python documentation

if
The if statement is Python's primary conditional control structure. It allows your program to make decisions by executing different code blocks based on whether a condition is True or False. The if statement is fundamental to creating dynamic, responsive programs that can adapt their behavior based on various conditions, user input, or data states.
for
The for loop in Python is used to iterate over sequences like lists, tuples, strings, or any iterable object. Unlike traditional for loops in other languages that use counters, Python's for loop is more like a "for each" loop, automatically handling the iteration details. It's one of the most commonly used constructs in Python for processing collections and repeating actions.
while
The while loop repeats a block of code as long as its condition remains True. Unlike a for loop, which iterates a known number of times over a sequence, a while loop is ideal when you don't know in advance how many iterations you'll need - like waiting for valid user input or running until some external condition changes. A while loop can also have an optional else clause that runs if the loop finishes normally, without hitting a break.
pass
pass is a null operation - a statement that does absolutely nothing when executed. It exists purely to satisfy Python's syntax, which requires every block (function, class, loop, conditional) to contain at least one statement. pass is commonly used as a temporary placeholder while you're still planning out code, or to intentionally leave a branch empty.