Skip to content

Beginner · Loops: repeating work · topic 11 of 50 · 20 min

break, continue and loop else

Explanation

break stops a loop at once, and continue skips the rest of the current round and moves to the next one. A loop can also have an else block, which runs only when the loop finished without a break. pass is a placeholder that does nothing.

break: stop early

break is a keyword that ends the loop immediately. Python jumps to the first line after the loop. You usually use it inside an if, to stop as soon as you have found what you were looking for.

python
for n in range(50, 100):
    if n % 7 == 0:
        print("First multiple of 7 from 50:", n)
        break
Prints 56 and stops. Without break, the loop would keep going up to 99.
Think of it like this: When you look for your roll number on a results notice board, you stop reading once you find it; that is break. When you scroll a playlist and skip the songs you do not like but keep listening after them; that is continue.

continue: skip this round

continue ends only the current round. Python skips the rest of the block and goes straight to the next item.

python
for n in range(1, 8):
    if n % 2 == 0:
        continue
    print(n, end=" ")
Even numbers are skipped. Prints 1 3 5 7.

Loop else: "no break happened"

A for or while loop can have an else block. The else block runs when the loop ends normally. If the loop was stopped by break, the else block is skipped. So you can read loop else as "if no break happened".

python
n = 29
for d in range(2, n):
    if n % d == 0:
        print(n, "is not prime, it divides by", d)
        break
else:
    print(n, "is prime")
A prime number divides only by 1 and itself. No d divides 29, so the loop never breaks and else prints 29 is prime.
Careful: The loop else must line up with the for (or while), not with the if inside it. If you indent it under the if, it becomes the else of the if, and the program means something completely different.

break inside while True

while True: makes a loop whose condition is always True. The only way out is a break. This is useful when the stopping point is easiest to check in the middle of the block.

python
balance = 1000
day = 0
while True:
    day += 1
    balance -= 180
    if balance < 180:
        break
print(f"Day {day}: only Rs {balance} left, not enough for one more lunch")
Spends 180 rupees a day and stops when there is not enough for the next day. Prints Day 5: only Rs 100 left.

pass: do nothing

Python does not allow an empty block. pass is a keyword that does nothing; it just fills the space. Use it as a placeholder for code you will write later.

python
for n in range(3):
    pass
print("The loop ran, but did nothing")
pass keeps the code valid while the block is still empty.
Tip: break and continue affect only the loop they are directly inside. In nested loops, a break in the inner loop does not stop the outer loop.

Watch the lesson

break, continue and loop else · 1/6

break · continue · else

In this lesson you will learn three ways to control a loop: break, continue, and the loop else.

Prefer a video by a teacher? Find free videos on “python break continue and loop else tutorial” on YouTube ↗

In real life: Finding the first free seat in a coach

A booking screen shows the seats of a train coach as a string: X means booked and O means free. The program looks seat by seat and stops at the first free one. If it reaches the end without finding one, the loop else says the coach is full.

python
seats = "XXXOXOXX"
for i in range(len(seats)):
    if seats[i] == "O":
        print("First free seat:", i + 1)
        break
else:
    print("Coach is full")
Prints First free seat: 4. Seat numbers start at 1, so we print i + 1.

Practice (0/3 done)

Task 1 · Code · Python

Write a function first_space(text) that returns the position (index) of the first space in text, or -1 if there is no space. Use a loop over range(len(text)) and stop at the first space. Example: first_space("Ravi Kumar") returns 4.

Task 2 · Code · Python

Write a function is_prime(n) that returns True if n is a prime number and False otherwise. A prime is a whole number greater than 1 that divides only by 1 and itself. Numbers below 2 are not prime.

Task 3 · Code · Python

Write a function no_spaces(text) that returns the text with all spaces removed. Build a new string in a loop and use continue to skip each space. Example: no_spaces("Mumbai Central") returns "MumbaiCentral".

Quick quiz

Question 1

1.What does break do inside a loop?

Question 2

2.What does continue do inside a loop?

Question 3

3.When does the else block of a for loop run?

Question 4

4.What does this print? for i in range(1, 6): if i == 3: continue, then print(i, end=" ")

Question 5

5.What does pass do?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: while loops