Loop Control
Learn how to control the flow of loops in Python using break, continue, and else.
Loop Control
Sometimes you need more control over what a loop does. Maybe you want to stop early when you find what you are looking for. Maybe you want to skip certain items. Maybe you want to run some code only if the loop finished without interruption. Python gives you three tools for this — break, continue, and else.
break — stop the loop early
break exits the loop immediately. No more iterations, no matter how many items are left.
numbers = [3, 7, 1, 9, 4, 6]
for number in numbers:
if number == 9:
print("Found 9 — stopping.")
break
print(number)Output:
3
7
1
Found 9 — stopping.Once Python hit 9, it ran the break and jumped out of the loop. 4 and 6 were never touched.
Without break the loop would have printed every number. With break you exit the moment the job is done.
A practical example — searching for a user in a list:
users = ["ali", "sara", "omar", "fatima"]
search = "omar"
for user in users:
if user == search:
print(f"Found user: {user}")
break
else:
print("User not found.")More on that else in a moment.
continue — skip to the next iteration
continue skips the rest of the current iteration and jumps straight to the next one. The loop does not stop — it just skips that one item.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for number in numbers:
if number % 2 == 0:
continue
print(number)Output:
1
3
5
7
9Every time number is even, continue fires and Python skips the print. The loop keeps going with the next number.
A real use — processing only valid entries and ignoring the bad ones:
data = ["ali", "", "sara", " ", "omar"]
for name in data:
if not name.strip():
continue
print(f"Processing: {name}")Output:
Processing: ali
Processing: sara
Processing: omarname.strip() removes whitespace. If the result is empty, not name.strip() is true — so we skip it with continue and move on.
break vs continue
These two are easy to mix up. Here is the clear difference:
| What it does | Loop continues? | |
|---|---|---|
break | Exits the loop entirely | No |
continue | Skips this iteration only | Yes |
numbers = [1, 2, 3, 4, 5]
# break — stops at 3
for n in numbers:
if n == 3:
break
print(n)
# 1
# 2
# continue — skips 3
for n in numbers:
if n == 3:
continue
print(n)
# 1
# 2
# 4
# 5else on a loop
This one surprises most beginners. In Python, both for and while loops can have an else block. The else runs only if the loop finished normally — meaning it was never interrupted by a break.
numbers = [1, 3, 5, 7, 9]
for number in numbers:
if number == 4:
print("Found 4.")
break
else:
print("4 was not in the list.")Output:
4 was not in the list.The loop went through every number without hitting break — so the else ran.
Now with a number that exists:
numbers = [1, 3, 4, 7, 9]
for number in numbers:
if number == 4:
print("Found 4.")
break
else:
print("4 was not in the list.")Output:
Found 4.This time break fired — so the else was skipped entirely.
Think of loop else as "no break". It answers the question — did this loop finish without being interrupted? If yes, run the else block.
The same works with while loops:
attempts = 0
while attempts < 3:
password = input("Enter password: ")
attempts += 1
if password == "secret123":
print("Access granted!")
break
else:
print("Too many failed attempts. Locked out.")Output if the user fails all 3 times:
Enter password: abc
Enter password: 123
Enter password: hello
Too many failed attempts. Locked out.Output if the user gets it right:
Enter password: secret123
Access granted!The else only runs if the while loop ran out of attempts without a successful break.
Putting it all together
A real example that uses all three — searching a list of orders, skipping cancelled ones, stopping when the target is found:
orders = [
{"id": 1, "status": "shipped"},
{"id": 2, "status": "cancelled"},
{"id": 3, "status": "delivered"},
{"id": 4, "status": "cancelled"},
{"id": 5, "status": "pending"},
]
target_id = 3
for order in orders:
if order["status"] == "cancelled":
continue # skip cancelled orders
if order["id"] == target_id:
print(f"Found order {order['id']} — status: {order['status']}")
break # stop once found
else:
print(f"Order {target_id} not found.")Output:
Found order 3 — status: deliveredSummary
| Tool | Purpose | Loop continues? |
|---|---|---|
break | Exit the loop immediately | No |
continue | Skip current iteration, move to next | Yes |
else | Run if loop finished without a break | — |