break, continue and else
Let's go back to the running track. So far, the runner stopped only at the arch, when it did not let them through. But sometimes the goal is reached in the middle of the run. Then the runner leaves the track at once, without waiting for the end.
A loop that searches for something behaves the same way: it should stop as soon as it finds it. Otherwise it checks numbers that are no longer needed. The break statement ends a loop at once. This program finds the first number greater than 50 that is divisible by 7:
for n in range(51, 101):
if n % 7 == 0:
print(n) # 56
breakTwo ways to change a loop's course
Python has two statements that change the usual order of iterations. Both work in for and in while.
break
break is the runner leaving the race. After break, the lines below it in the body do not run, and no further iterations follow. The program continues from the line after the loop.
Let's walk through the example above iteration by iteration:
- For
nfrom51to55, the conditionn % 7 == 0is false, and the loop goes on. - When
nis56, the condition is true: the program prints56. breakends the loop, and the numbers from57to100are never checked.
The break sits inside the if, but it ends the whole loop, not only the if. Without break, the program would print every such number up to 100: 56, 63, 70 and so on up to 98.
continue
continue is the runner cutting the rest of the current lap short and heading straight to the arch for the next one. The loop does not end: for takes the next value and starts the body again.
for n in range(1, 7):
if n % 3 == 0:
continue
print(n, end=" ") # 1 2 4 5Let's go through the iterations:
nis1: the condition is false, andprintprints1.nis2:2is printed the same way.nis3: the condition is true,continueskips theprint, and the loop takes4.4and5are printed.nis6:continueagain, and with that the series runs out.
The numbers print on one line, because end=" " puts a space in place of the line break. If you replace continue with break, the program prints only 1 2: the loop ends when n is 3.
break and continue in while
In a while True: loop, break is the usual way out once the goal is reached. A continue in a while needs care. Change the counter before the continue, or the next iteration starts with the same value, and the loop may become infinite.
else on a loop
A for or while loop can have an else branch. It runs when the loop ends by itself, and it is skipped if a break stopped the loop. On the track, it is the finisher's medal: the runner who made it to the end gets one, and a runner who left the race goes without.
The word else is written at the same level as for, at the same indentation. This is how a search reports that it found nothing:
for n in range(8, 14):
if n % 7 == 0:
print("found", n)
break
else:
print("not found") # not foundLet's see what happens here:
range(8, 14)gives the numbers from8to13.- None of them is divisible by
7, so the body of theifnever runs. - The series runs out, the loop ends by itself, and the
elsebranch printsnot found.
With range(8, 15), 14 enters the series. The program prints found 14, break stops the loop, and the else is skipped.
The name is misleading: "else" sounds as if the branch runs when the loop did not run. In fact, a loop's else means "if there was no break". Interviewers often ask about this.
Common mistake
The else branch is written at the indentation of the if, not the for:
for n in range(8, 15):
if n % 7 == 0:
print("found", n)
break
else: # ✗
print("not found")There is no error, but this else belongs to the if and runs on every iteration where the condition is false. The program prints not found six times—for the numbers from 8 to 13—and only then found 14.
Key points
breakends the whole loop at once, even when it sits inside anif.continueskips only the rest of the current iteration, and the loop takes the next value.- A loop's
elsebranch runs when the loop ends by itself, with nobreak. - A loop's
elseis written at the indentation of thefororwhile, not theif.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.