Skip to content
Kuza Ndoto Academy

Break, Continue & Pass

Beginner → IntermediateChapter 3 · Page 17 of 564 min readUpdated 15 Nov 2026

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")
Checked 4 Checked 9 Checked 15 Found a number over 20: 22 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")
1) Balance 2) Exit: 1 Your balance is KES 2,300 1) Balance 2) Exit: 5 Invalid choice 1) Balance 2) Exit: 2 Goodbye!

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)
1 3 5 7

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())
AMINA BRIAN CHEBET
Common mistake

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")
Kisumu 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
Book a revision session

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.

more items? yes ...statements... if skip: continue if done: break rest of body end of body: next item continue break no more items (loop else runs) code after the loop
Figure 1. 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.
StatementWhat it doesTypical use
breakExits the innermost loop immediatelyStop searching once found; leave a menu
continueSkips the rest of this pass and starts the nextIgnore invalid or unwanted items
passDoes nothingPlaceholder for code you'll write later
loop elseRuns if the loop ended without breakReport "not found" after a search
Note

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

  • break ends the innermost loop immediately; pair it with while True for menus.
  • continue skips the rest of the current iteration and moves to the next.
  • A loop's else block runs only if the loop wasn't ended by break.
  • pass does nothing and fills blocks you haven't written yet.
Up next · Control FlowNested LoopsLoops inside loops, and when to use them.
→

Tip: use the ← and → keys to move between pages.