Control FlowBeginner7 min30 / 63

break, continue & pass

Learn how to take control of your loops by stopping them early, skipping steps, or leaving a placeholder for code you'll write later.

Loops are great — they let you repeat work automatically. But sometimes you need to jump out early, skip a bad item, or just hold a spot for code you haven't written yet.

Python gives you three small keywords for exactly this: break, continue, and pass. Each one is short, but each one changes how your loop behaves in a different way.

See it in action

Visual walkthrough1 / 6
1

Take Control of Your Loops

Loops repeat — but sometimes you need to stop early, skip a step, or just hold a spot. Python's break, continue, and pass give you that control.

These three tiny keywords unlock a whole new level of loop power.

#break — Exit the Loop Immediately

Think of it like

Think of a fire alarm

You're searching every drawer in your desk for your keys. The moment you find them, you stop looking — you don't open the rest of the drawers. That's exactly what break does: it says "I'm done here, stop the loop right now."

Use break when you've found what you were looking for and there's no point continuing. The loop ends immediately, and Python moves on to the code that comes after the loop.

The loop never reaches Diana because break exits as soon as Charlie is found.
names = ["Alice", "Bob", "Charlie", "Diana"]

for name in names:
    if name == "Charlie":
        print("Found Charlie! Stopping.")
        break
    print(f"Checking {name}...")

#continue — Skip to the Next Iteration

continue does not stop the loop. Instead, it says: "Skip the rest of this step and move on to the next one." The loop keeps going — you just skip whatever comes after continue for this particular item.

Think of it like

Think of a quality inspector

You're checking apples on a conveyor belt. When you spot a bruised one, you toss it aside and move on — you don't shut down the whole belt. continue is that "toss it aside" moment.

Negative numbers are skipped; the loop keeps running for the rest.
numbers = [1, -3, 7, -2, 5]

for n in numbers:
    if n < 0:
        continue  # skip negative numbers
    print(f"Processing {n}")

#pass — Do Nothing (a Placeholder)

pass is the quiet one. It does absolutely nothing — it's a placeholder you use when Python requires a code block, but you're not ready to write the code yet.

Python doesn't allow empty blocks. If you write an if or a for with nothing inside, you'll get an error. pass is how you tell Python: "There's nothing here on purpose."

pass lets the loop run without error, even though the body is empty.
for i in range(5):
    pass  # TODO: fill this in later

print("Loop finished without doing anything.")
Tip

pass is common while sketching code

When you're planning the structure of a program, you might write several if branches or functions and fill them with pass as a reminder to come back. It's a perfectly normal thing to do.

#The Loop-else Clause

Here's a feature many beginners don't know about: a for or while loop can have an else block. The else runs only if the loop completed without hitting a `break`.

This is useful for "search" patterns — if you searched through everything and never broke out, you know you didn't find it.

The else block runs because we never hit break — the target wasn't found.
fruits = ["apple", "banana", "mango"]
target = "grape"

for fruit in fruits:
    if fruit == target:
        print(f"Found {target}!")
        break
else:
    print(f"{target} was not in the list.")
Common mistake

Loop-else is NOT like if-else

The else on a loop runs when the loop finishes normally (no break). It does not run when the loop body is skipped or when a continue is used. If a break fires, the else is skipped entirely. This surprises many beginners — remember the rule: else = "no break happened".

#Real Example: Validating User Input

Here's a practical example that combines continue and break together. We process a list of scores, skip anything invalid, and stop early if we find a perfect score.

continue skips bad data; break stops when the goal is reached.
scores = [72, -5, 88, 101, 95, 100, 60]

for score in scores:
    if score < 0 or score > 100:
        print(f"Skipping invalid score: {score}")
        continue
    if score == 100:
        print("Perfect score found! Stopping early.")
        break
    print(f"Valid score: {score}")
Quick check

What does the else block in a for-else construct do?

Key takeaways

  • `break` exits the entire loop immediately — use it when you've found what you need.
  • `continue` skips the rest of the current iteration and moves to the next one — use it to filter out unwanted items.
  • `pass` is a silent placeholder that does nothing — use it when Python requires a block but you have nothing to put there yet.
  • A loop's `else` block runs only if the loop finished without a `break` — perfect for search patterns.
Practice challenges
Test yourself · earn XP
0/4
Predict the output#1

What does this code print?

predict-output
numbers = [10, 20, 30, 40, 50]

for n in numbers:
    if n == 30:
        break
    print(n)
Fix the bug#2

This code is supposed to print only the positive numbers, but it prints all of them. What is wrong?

fix-bug
nums = [3, -1, 7, -4, 2]

for n in nums:
    if n < 0:
        break
    print(n)
Fill in the blank#3

Complete the code so it prints every word except "skip".

words = ["hello", "skip", "world"]

for word in words:
    if word == "skip":
        
    print(word)
Reorder the lines#4

Put these lines in the right order so the code searches for "mango" and prints a message if it is not found.

1
else:
2
for fruit in fruits:
3
    print("mango not found")
4
fruits = ["apple", "banana", "cherry"]
5
        break
6
    if fruit == "mango":
Your turn
Practice exercise

Write a program that searches a list of passwords for the first one that is at least 8 characters long AND contains a digit. Use a for loop with break and continue. If a password is too short, print a message and skip it with continue. When you find a valid password, print it and break. Use loop-else to print a message if no valid password was found.

Test list: ["hi", "hello", "pass1234", "abc123"]

Try it live — edit the code and hit Run to execute real Python:

solution.py · editable