Python break, continue, and loop else Explained

break stops a loop early, continue skips the rest of the current iteration, and else on a loop runs only if the loop finishes without a break. These three tools let you control loop flow without extra flags or nested conditionals. The rest of this article shows how each one works, how they differ, and the mistakes students make most often.

Difficulty: Beginner

Helpful to know first: basic for and while loops, if statements, and how indentation controls blocks in Python.

How break, continue, and loop else Change Loop Flow

break and continue are loop control statements. They do not change the condition of the loop; they change what happens inside the loop body while Python is running it.

break exits the nearest enclosing for or while loop immediately. Python jumps to the first statement after that loop.

continue ends the current loop iteration early. Python skips any remaining statements in the loop body and moves on to the next item or next condition check.

A loop else block is different from an if else. It belongs to the loop itself, not to a condition inside the loop. Python runs the loop else only when the loop finishes normally, which means it did not stop because of break.

The loop else block is often used with search loops: “if we never broke out early, then the item was not found.”

Here is the smallest useful example of each one:

Python
for number in range(5):
    if number == 2:
        break
    print(number)

# Output:
# 0
# 1

This loop stops completely when number becomes 2. The values after that are never processed.

Python
for number in range(5):
    if number == 2:
        continue
    print(number)

# Output:
# 0
# 1
# 3
# 4

This loop keeps going, but it skips printing 2. The loop itself still reaches the end.

Python
for number in range(3):
    print(number)
else:
    print("finished")

# Output:
# 0
# 1
# 2
# finished

This else runs because the loop ended normally. If a break had happened, Python would skip the else block.

Worked Examples of break, continue, and Loop else in Real Code

Stopping when a value is found

A common use for break is search. You loop through items until you find the one you want, then stop immediately to avoid unnecessary work.

Python
names = ["Ada", "Grace", "Linus"]

for name in names:
    if name == "Grace":
        print("Found Grace")
        break
else:
    print("Grace was not found")

# Output:
# Found Grace

The else does not run here because break ended the loop early. This pattern is useful because it avoids a separate “found” flag variable.

Skipping invalid data but keeping the loop going

continue is useful when some items should be ignored, but the rest still need processing. This is common when reading input or filtering values.

Python
temperatures = [21, -999, 19, 23]

for temperature in temperatures:
    if temperature == -999:
        continue
    print(f"Valid temperature: {temperature}")

# Output:
# Valid temperature: 21
# Valid temperature: 19
# Valid temperature: 23

The invalid marker is skipped, but the loop still processes later items. This is often cleaner than nesting the useful code inside an else block after a negative check.

Using loop else to report a missing item

The loop else block makes search logic read naturally: if no break happened, then the item was never found.

Python
numbers = [4, 7, 9]
target = 8

for number in numbers:
    if number == target:
        print("Found it")
        break
else:
    print("Not found")

# Output:
# Not found

Without else, you would usually need a separate variable such as found = False. The loop else makes the intent clearer when the only reason to stop early is a successful match.

Cleaning a while loop with continue

continue also works in while loops. The important detail is that any code that updates the loop condition must still happen, or the loop can get stuck forever.

Python
number = 0

while number < 5:
    number += 1
    if number == 3:
        continue
    print(number)

# Output:
# 1
# 2
# 4
# 5

The loop still increments number before the continue. That is why the loop ends correctly instead of repeating forever.

Nested loops and breaking only the nearest one

break exits only the closest loop. If you have nested loops, the outer loop keeps running unless you stop it some other way.

Python
for row in range(2):
    for column in range(3):
        if column == 1:
            break
        print(row, column)

# Output:
# 0 0
# 1 0

Only the inner loop stops at column == 1. The outer loop continues with the next row.

How break, continue, and loop else Behave in Detail

Python checks break and continue while the loop body is running. They do not wait for the loop condition to be tested again. When Python reaches break, it leaves the loop immediately. When it reaches continue, it jumps to the next iteration.

The loop else has one important rule: it runs only if the loop did not end through break. Normal exhaustion counts as success for else, even if the loop body never ran.

That last detail surprises many beginners. A for loop over an empty list still counts as finishing normally, so its else block runs.

Python
for item in []:
    print(item)
else:
    print("Loop finished normally")

# Output:
# Loop finished normally

The loop did not break, so the else still runs. This is one reason loop else is about loop completion, not about whether the loop body executed.

Another detail is that continue does not prevent the loop else from running. Only break blocks the else. If every iteration uses continue and the loop still finishes, the else runs as usual.

In a while loop, make sure the code that changes the loop condition still happens before any continue that could skip it. Otherwise the loop may never end.

break and continue are allowed only inside loops. If you write them outside a loop, Python raises a syntax error before the program runs.

Python
break
# SyntaxError: 'break' outside loop

The same rule applies to continue:

Python
continue
# SyntaxError: 'continue' not properly in loop

The meaning is simple: Python needs a loop context to know where to jump.

Predict the Output

Will the else block run after a continue?

Read the code carefully and decide what it prints.

Python
for number in range(3):
    if number == 1:
        continue
    print(number)
else:
    print("done")

# What does this print?
Show answer

It prints 0, 2, then done.

The loop visits 0, prints it, then visits 1 and hits continue, so that iteration prints nothing. The loop still reaches 2, prints it, and then ends normally. Because there was no break, the else block runs.

Terminal
# Output:
# 0
# 2
# done

Does break stop the else block?

Now compare that with a loop that stops early.

Python
for number in range(5):
    if number == 2:
        break
    print(number)
else:
    print("finished")

# What does this print?
Show answer

It prints 0 and 1.

The loop stops as soon as number == 2. Because the loop ended with break, Python skips the else block.

Terminal
# Output:
# 0
# 1

Common Mistakes with break, continue, and loop else

Using continue when you meant to stop the loop

Students often use continue when they really want break. The two keywords do very different things: one skips an iteration, the other ends the loop.

Problem: This code expects to stop when it finds the first negative value, but continue only skips that one item and keeps going.

Python
numbers = [5, 3, -1, 8]

for number in numbers:
    if number < 0:
        continue
    print(number)

Fix: Use break if the loop should end as soon as the negative value appears.

Python
numbers = [5, 3, -1, 8]

for number in numbers:
    if number < 0:
        break
    print(number)

The corrected version works because break exits the loop immediately instead of only skipping one iteration.

Expecting else to pair with if instead of the loop

Many beginners read the loop else as if it belongs to the last if inside the loop. It does not. The indentation can make this mistake easy to miss.

Problem: This code looks as if else belongs to the if, but it actually belongs to the for loop.

Python
for number in range(3):
    if number == 1:
        print("middle")
else:
    print("done")

Fix: Indent the else correctly only when you want the loop-level else. If you want an if else, keep it inside the loop block.

Python
for number in range(3):
    if number == 1:
        print("middle")
    else:
        print("not middle")

The corrected version makes the structure clear: the first block is a loop else, while the second is an if else.

Skipping the counter update in a while loop

A continue inside a while loop can cause an infinite loop if it skips the statement that changes the loop condition.

Problem: When number is 2, the loop skips the increment. That means number stays 2 forever, so the loop never ends.

Python
number = 0

while number < 5:
    if number == 2:
        continue
    number += 1
    print(number)

Fix: Update the counter before the continue, or redesign the loop so the condition always changes.

Python
number = 0

while number < 5:
    number += 1
    if number == 2:
        continue
    print(number)

The fixed version works because the loop variable changes on every pass, so the loop eventually reaches its end.

Putting break or continue outside a loop

break and continue only make sense inside a loop. If they appear in the wrong place, Python raises a syntax error before running anything.

Problem: This code has no enclosing loop, so Python cannot interpret the jump statement.

Python
if True:
    break

# SyntaxError: 'break' outside loop

Fix: Move the statement inside a for or while loop.

Python
for number in range(3):
    if number == 1:
        break
    print(number)

The fixed version works because break now has a loop to exit.

Find the Bug

A student wanted to print every number up to a target, but stop when the target is reached. The code looks close, but one line is wrong. Find the bug.

Python
target = 4

for number in range(6):
    if number == target:
        continue
    print(number)
else:
    print("Reached the end")
Show answer

The bug is the use of continue instead of break. The code skips printing 4, but it keeps going to 5 and then runs the loop else because the loop ended normally.

Fixed code:

Python
target = 4

for number in range(6):
    if number == target:
        break
    print(number)
else:
    print("Reached the end")

The fixed version works because break stops the loop before the target value is processed, and it also prevents the loop else from running.

When to Use break, continue, and loop else

Use these statements when they make the flow easier to read, not just because you can. In many loops, a plain if block is enough. In others, these tools remove extra variables and nested logic.

NeedBest choiceWhy
Stop as soon as a match is foundbreakEnds the loop immediately and avoids unnecessary work.
Ignore one item and keep processing the restcontinueSkips only the current iteration.
Report success only if the loop finished without a matchLoop elseMatches search patterns without a separate flag.

Loop else is most useful when the loop is searching for something. If the loop has unrelated work inside it, the else may feel confusing. In that case, a normal variable like found = False can be easier for readers.

For nested loops, remember that break only exits the nearest loop. If you need to stop multiple loops, use a clearer structure such as a function return, a flag checked by the outer loop, or a different algorithm.

Compared with a flag variable

Loop else can replace a flag in search code, but the flag is sometimes easier for beginners to understand.

Terminal
# With a flag
found = False
for item in items:
    if item == target:
        found = True
        break

if not found:
    print("Not found")

The loop else version is shorter, but the flag version may be easier to read when you are still learning loops. Choose the one that makes intent clearest to your reader or team.

Try It Yourself

Write a loop that checks the numbers 1 through 5 and prints only the odd ones. Then use a loop else to print "Done" after the loop finishes normally.

  • Use a for loop.
  • Skip even numbers with continue.
  • Do not use break.
  • Print each odd number on its own line.
  • Print Done after the loop.

Expected output:

Python
1
3
5
Done

Hint: Use number % 2 == 0 to detect even numbers.

Show solution
Python
for number in range(1, 6):
    if number % 2 == 0:
        continue
    print(number)
else:
    print("Done")

Summary

  • break stops the nearest loop immediately.
  • continue skips the rest of the current iteration and moves on.
  • Loop else runs only if the loop ended without break.
  • continue does not stop a loop else from running.
  • break and continue must be inside a loop or Python raises a syntax error.
  • Loop else is most useful in search-style code.

Next, study nested loops and how to control more than one loop at a time. That topic builds directly on the behavior of break and continue.