Skip to content
Theory · Loops · lesson 4 of 7

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:

Python
for n in range(51, 101):
    if n % 7 == 0:
        print(n)  # 56
        break

Two 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:

  1. For n from 51 to 55, the condition n % 7 == 0 is false, and the loop goes on.
  2. When n is 56, the condition is true: the program prints 56.
  3. break ends the loop, and the numbers from 57 to 100 are 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.

Python
for n in range(1, 7):
    if n % 3 == 0:
        continue
    print(n, end=" ")  # 1 2 4 5

Let's go through the iterations:

  1. n is 1: the condition is false, and print prints 1.
  2. n is 2: 2 is printed the same way.
  3. n is 3: the condition is true, continue skips the print, and the loop takes 4.
  4. 4 and 5 are printed.
  5. n is 6: continue again, 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:

Python
for n in range(8, 14):
    if n % 7 == 0:
        print("found", n)
        break
else:
    print("not found")  # not found

Let's see what happens here:

  1. range(8, 14) gives the numbers from 8 to 13.
  2. None of them is divisible by 7, so the body of the if never runs.
  3. The series runs out, the loop ends by itself, and the else branch prints not 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:

Python
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

  • break ends the whole loop at once, even when it sits inside an if.
  • continue skips only the rest of the current iteration, and the loop takes the next value.
  • A loop's else branch runs when the loop ends by itself, with no break.
  • A loop's else is written at the indentation of the for or while, not the if.

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way