We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

Python Break vs Continue: What's the Difference?

Lane Wagner
Lane WagnerBoot.dev co-founder and backend engineer

Last published

Table of Contents

Sometimes, while looping through a sequence, you may find items that you want to skip. Python provides a way to do this: the continue statement. But what if you want to exit the loop entirely? That's where the break statement comes in.

All the content from our Boot.dev courses are available for free here on the blog. This one is from the "Loops" chapter of Learn Python for Beginners. If you want to try the far more immersive version of the course, do check it out!

Python break vs continue

Statement What it does
continue Skips the rest of the current iteration
break Exits the loop entirely

What Does continue Do?

continue means "go directly to the next iteration of this loop." Whatever else was supposed to happen in the current iteration is skipped.

For example, if we're calculating square roots, we might want to skip negative numbers. continue lets us move on to the next number without wasting any time:

for number in range(-5, 5):
    if number < 0:
        continue  # Skip negatives

    print(f"The square root of {number} is {number**0.5}")

This would print:

The square root of 0 is 0.0
The square root of 1 is 1.0
The square root of 2 is 1.4142135623730951
The square root of 3 is 1.7320508075688772
The square root of 4 is 2.0

A continue statement immediately halts the current iteration and jumps to the next one, which saves the program from doing unnecessary work.

What Does break Do?

We can use continue to skip to the next iteration in a loop, but what if we want to exit the loop entirely? That's where the break statement comes in.

for n in range(42):
    print(f"{n} * {n} = {n * n}")
    if n * n > 150:
        break

This code would loop from 0 all the way to 41, but it actually exits early. Once n * n is greater than 150, the break statement executes, stopping the loop. The last line printed is 13 * 13 = 169.

Use continue when you want to skip one iteration but keep looping. Use break when the loop should stop. The Python loops overview covers for, while, and range().

Frequently Asked Questions

What is the difference between break and continue in Python?

continue skips the rest of the current iteration and starts the next one. break exits the loop entirely.

Do break and continue work in both for and while loops?

Yes. Both statements work in Python for loops and while loops.

Does break exit all nested loops in Python?

No. break exits only the innermost loop that contains it. The surrounding loops continue normally.

When should I use continue instead of break?

Use continue when you want to skip one item but keep processing later items. Use break when the loop has finished its job and should stop.