Break, Continue & Pass
Normally a loop runs its whole block on every pass until it finishes. break and continue let you change that: break leaves the loop immediately, and continue skips straight to the next pass. pass is a placeholder that does nothing at all.
break: leave the loop early
When Python reaches break, it exits the innermost loop at once and carries on with the code after the loop. It's ideal for searching: once you've found what you want, there's no point continuing.
numbers = [4, 9, 15, 22, 8]
for n in numbers:
if n > 20:
print("Found a number over 20:", n)
break
print("Checked", n)
print("Search finished")
The loop never reaches 8. As soon as 22 is found, break ends the loop.
break with while True
A while True: loop would run forever, so it must contain a break. This pattern is perfect for menus, where you can't know how many times the user will choose an option. The output shows a sample run.
balance = 2300
while True:
choice = input("1) Balance 2) Exit: ")
if choice == "1":
print(f"Your balance is KES {balance:,}")
elif choice == "2":
print("Goodbye!")
break
else:
print("Invalid choice")
continue: skip to the next pass
continue stops the current iteration and jumps back to the top of the loop for the next one. The rest of the block is skipped only this time.
for n in range(1, 8):
if n % 2 == 0:
continue # skip even numbers
print(n)
It's useful for ignoring bad or irrelevant data without wrapping everything in a big if block:
names = ["Amina", "", "Brian", " ", "Chebet"]
for name in names:
if not name.strip():
continue # skip blank entries
print(name.upper())
Using continue in a while loop before the update step. In the code below, when i is 2, continue jumps back to the condition without ever reaching i += 1, so i stays 2 forever and the loop never ends. Update the counter before continue.
i = 0
while i < 5:
if i == 2:
continue # infinite loop: i += 1 is never reached
print(i)
i += 1
The else clause on loops
Loops can have an else block. It runs when the loop finishes normally, meaning it was not stopped by break. This neatly answers the question "did my search find anything?"
target = "Kisumu"
cities = ["Nakuru", "Eldoret", "Thika"]
for city in cities:
if city == target:
print("Found", target)
break
else:
print(target, "is not in the list")
If "Kisumu" were in the list, break would run and the else block would be skipped. The same else works on while loops. The name is confusing, so think of it as "no break".
pass: do nothing
Python requires every block to contain at least one statement. pass is a statement that does nothing, so it's used as a placeholder while you're still writing code.
marks = 45
if marks >= 50:
pass # TODO: print a certificate later
else:
print("Book a revision session")
for day in range(3):
pass # loop body to be written
Without pass, an empty block causes an IndentationError. You'll also use pass in empty functions and classes.
Comparison
The flowchart shows where each statement sends the program inside a for loop.
continue (dashed) skips the rest of the body and goes straight back to fetch the next item. break (red) leaves the loop at once, skipping any loop else. A loop that runs out of items ends normally, which is when else runs.| Statement | What it does | Typical use |
|---|---|---|
break | Exits the innermost loop immediately | Stop searching once found; leave a menu |
continue | Skips the rest of this pass and starts the next | Ignore invalid or unwanted items |
pass | Does nothing | Placeholder for code you'll write later |
loop else | Runs if the loop ended without break | Report "not found" after a search |
break and continue only affect the loop they are directly inside. In a loop within a loop, break leaves the inner loop only; see Nested Loops for ways to exit both.
Key points
breakends the innermost loop immediately; pair it withwhile Truefor menus.continueskips the rest of the current iteration and moves to the next.- A loop's
elseblock runs only if the loop wasn't ended bybreak. passdoes nothing and fills blocks you haven't written yet.
Tip: use the ← and → keys to move between pages.
