Python break, continue, and pass Explained

break exits a loop, continue skips to the next pass, and pass does nothing at all. See how each one works in for and while loops, and when to use them.

4 min read

break, continue, and pass are three small Python statements that are easy to mix up:

StatementWhat it does
breakLeaves the loop immediately
continueSkips the rest of the current pass and starts the next one
passDoes nothing; it fills a block that would otherwise be empty

break and continue only work inside a for or while loop. pass can go anywhere a statement is required.

break: leave the loop now

break ends the loop at once. Python continues with the first line after the loop.

pythonpython
numbers = [4, 9, 15, 22, 7]
 
for number in numbers:
    if number > 10:
        print("First number above 10:", number)
        break

This prints First number above 10: 15. The loop never looks at 22 or 7, because there is no reason to keep searching once the answer is found.

break is also the normal way out of a while True loop:

pythonpython
total = 0
 
while True:
    entry = input("Enter a number (blank to finish): ")
    if entry == "":
        break
    total += float(entry)
 
print("Total:", total)

continue: skip to the next pass

continue abandons the current pass. In a for loop Python moves to the next item. In a while loop it goes back to check the condition.

pythonpython
for number in range(1, 8):
    if number % 2 == 0:
        continue
    print(number)

This prints 1, 3, 5, and 7. Each even number triggers continue, so the print line is skipped for it.

continue is useful for ignoring items you do not want to process:

pythonpython
lines = ["alice@example.com", "", "# comment", "bob@example.com"]
 
for line in lines:
    if line == "" or line.startswith("#"):
        continue
    print("Sending to", line)

In a while loop, make sure the update runs before continue. If continue skips the line that changes the loop variable, the condition never changes and the loop runs forever.

pythonpython
n = 0
 
while n < 5:
    n += 1          # update first
    if n == 3:
        continue
    print(n)        # prints 1, 2, 4, 5

pass: do nothing

pass is a statement that has no effect. Python does not allow an empty block, so pass stands in when a block is required but you have nothing to put there yet.

pythonpython
def send_invoice(order):
    pass  # to be written later
 
 
for item in ["a", "b", "c"]:
    pass  # an empty loop is valid with pass

Without pass, both blocks would be an IndentationError.

pass is also used to deliberately ignore a case:

pythonpython
value = "42a"
 
try:
    number = int(value)
except ValueError:
    pass  # not a number; leave it unconverted

pass is not continue

This is the difference people most often get wrong. continue jumps to the next pass. pass does nothing, so the code below it still runs.

pythonpython
for n in range(1, 4):
    if n == 2:
        pass
    print(n)      # prints 1, 2, 3
 
for n in range(1, 4):
    if n == 2:
        continue
    print(n)      # prints 1, 3

break and continue in nested loops

In a loop inside another loop, break and continue affect only the innermost loop that contains them:

pythonpython
for row in range(1, 4):
    for col in range(1, 4):
        if col == 2:
            break
        print(row, col)

This prints 1 1, 2 1, and 3 1. The break ends the inner loop each time, but the outer loop carries on with the next row. Ways to leave both loops at once are covered in nested loops in Python.

break and the loop else clause

A loop's else block runs only if the loop was not ended by break. That makes a neat "not found" branch:

pythonpython
users = ["ada", "grace", "linus"]
 
for user in users:
    if user == "guido":
        print("Found guido.")
        break
else:
    print("guido is not in the list.")

When to use each one

  • Use break when you have found what you were looking for or cannot continue.
  • Use continue to skip an item early and keep the main logic of the loop unindented.
  • Use pass as a placeholder while you are still writing the code, or to say clearly that nothing should happen.

Use break and continue sparingly. A loop with several of each is hard to follow, and a clearer condition is often the better fix. For the loops themselves, see Python for loops explained.

Rune AI

Rune AI

Key Insights

  • break leaves the loop immediately; code after the loop runs next.
  • continue skips the rest of the current pass and moves to the next one.
  • pass does nothing and exists to fill a block that cannot be left empty.
  • pass is not continue: the lines after pass still run.
  • In nested loops, break and continue affect only the innermost loop.
  • A loop's else block runs only when the loop was not ended by break.
RunePowered by Rune AI

Frequently Asked Questions

What is the difference between break and continue in Python?

break ends the loop completely, and Python carries on with the code after the loop. continue ends only the current pass, and the loop goes on with the next item or the next condition check.

What is the difference between pass and continue in Python?

pass does nothing, so the rest of the loop body still runs. continue skips the rest of the loop body for the current pass. Use pass as an empty placeholder and continue to skip an item.

What does pass do in Python?

pass is a statement that has no effect. It is used where Python requires a block, such as the body of a function, class, loop, or if statement, but you have no code to put there yet.

Does break exit all loops in Python?

No. break exits only the innermost loop that contains it. To leave several nested loops, put them in a function and use return, or set a flag that the outer loop checks.

Can I use break outside a loop?

No. Using break or continue outside a for or while loop is a SyntaxError. To stop a function early use return, and to stop a whole program use sys.exit().

Conclusion

break stops a loop, continue skips one pass, and pass is an empty placeholder. Reach for break when the work is finished early, continue when an item should be ignored, and pass when a block has nothing to do yet.