Beginner · Loops: repeating work · topic 11 of 50 · 20 min
break, continue and loop else
Explanation
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.
for n in range(50, 100):
if n % 7 == 0:
print("First multiple of 7 from 50:", n)
breakcontinue: skip this round
continue ends only the current round. Python skips the rest of the block and goes straight to the next item.
for n in range(1, 8):
if n % 2 == 0:
continue
print(n, end=" ")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".
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")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.
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")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.
for n in range(3):
pass
print("The loop ran, but did nothing")Watch the lesson
break, continue and loop else · 1/6
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.
seats = "XXXOXOXX"
for i in range(len(seats)):
if seats[i] == "O":
print("First free seat:", i + 1)
break
else:
print("Coach is full")Practice (0/3 done)
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.
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.
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
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.