Course Progress22% complete
🐍 Python Basics Topic 22 / 100
⏱ 8 min read

break and continue

Two keywords that give you fine control over loops — exit early with break, or skip one item with continue.

"break says 'I'm done — exit the whole loop'. continue says 'skip this one item — move on to the next'. Two words, enormous power."

— ShurAI

The Two Keywords at a Glance

🛑
break
Immediately exits the entire loop. No more iterations. Code after the loop runs next.
⏭️
continue
Skips the rest of this iteration only. The loop keeps going with the next item.

break — Exit the Loop Early

Imagine walking down a street looking for a shop. The moment you find it, you stop walking. You don't need to check every remaining shop. That's break:

python — find first negative number
numbers = [4, 7, 2, -3, 8, -1, 5]

for n in numbers:
    if n < 0:
        print(f"Found negative: {n} — stopping!")
        break               # exit immediately
    print(f"Checked {n} — ok")

print("Loop finished.")
output
Checked 4 — ok
Checked 7 — ok
Checked 2 — ok
Found negative: -3 — stopping!
Loop finished.

Notice: 8, -1, and 5 were never checked. Once break ran, Python jumped straight to "Loop finished."

What break does inside the loop:
4 ✓
7 ✓
2 ✓
-3 🛑 BREAK
8
-1
5
Greyed items are never reached after break.

continue — Skip One, Keep Going

Imagine sorting apples on a conveyor belt. When you spot a bad apple, you throw it away and move on — you don't stop the whole belt. That's continue:

python — skip negative numbers
numbers = [4, -3, 7, -1, 9, 2]

for n in numbers:
    if n < 0:
        continue            # skip negatives, keep going
    print(f"Processing: {n}")
output
Processing: 4
Processing: 7
Processing: 9
Processing: 2
What continue does — skips some, loop keeps going:
4 ✓
-3 ⏭️
7 ✓
-1 ⏭️
9 ✓
2 ✓
Yellow items are skipped but the loop continues to the rest.

Example — Password Attempts with break

python
correct  = "shurai123"
max_tries = 3

for attempt in range(1, max_tries + 1):
    pw = input(f"Attempt {attempt}/{max_tries} — Password: ")
    if pw == correct:
        print("✅ Correct! Welcome.")
        break
    else:
        print("❌ Wrong password.")
else:
    # This else belongs to the for loop!
    # It only runs if the loop completed WITHOUT hitting break
    print("🔒 Account locked after 3 failed attempts.")
for...else and while...else

Python has a special feature: you can put an else on a loop. The else block runs only if the loop completed normally — i.e. was never interrupted by a break. It's perfect for "did we find what we were looking for?" patterns.

Example — Print Only Even Numbers with continue

python
print("Even numbers from 1 to 20:")

for i in range(1, 21):
    if i % 2 != 0:    # if odd, skip it
        continue
    print(i, end=" ")
# 2 4 6 8 10 12 14 16 18 20

Real Example — Product Search 🔍

python
products = [
    ("Rice",      80,  True),   # (name, price, in_stock)
    ("Sugar",     45,  False),
    ("Dal",       120, True),
    ("Oil",       200, False),
    ("Flour",     60,  True),
]

print("In-stock items under Rs.100:")

for name, price, in_stock in products:
    if not in_stock:
        continue           # skip out-of-stock items
    if price >= 100:
        continue           # skip expensive items
    print(f"  ✅ {name} — Rs.{price}")
output
In-stock items under Rs.100:
  ✅ Rice — Rs.80
  ✅ Flour — Rs.60

"break and continue are like the emergency brake and the skip button — small tools, but knowing when to use them makes your loops elegant."

— ShurAI

🧠 Quiz — Question 1

What does break do inside a loop?

🧠 Quiz — Question 2

What does continue do inside a loop?

🧠 Quiz — Question 3

When does the else block on a for loop run?

🧠 Quiz — Question 4

In the numbers [1,2,3,4,5], looping with if n==3: break, which numbers are printed before the loop exits?