Lesson 03-10: The break Keyword

Learning Target: I can use the break keyword to end a loop early.

What does break do?

On occasion, there comes a time when you want to stop your loop right away. That’s what the break keyword does. Take the following as an example:

 
1
x = 7
2
3
while True:
4
    print(x)
5
    if x < 3:
6
        break
7
    x = x - 1
8
print("end")
9
7
6
5
4
3
2
end

(0310_ex_1)

This also works for for loops as well:

6
 
1
for x in range(7,0,-1):
2
    print(x)
3
    if x < 3:
4
        break
5
print("end")
6
7
6
5
4
3
2
end

(0310_ex_2)

The break keyword only quits out of the “innermost” loop. This means that if you have a loop inside of a loop (also covered later), using break will only quit out of the most recently-used loop.

When to use break?

break is not a keyword you should be using often in your programs. In general, you want to follow the following guidelines when you are thinking about using break:

  • Use break to exit a for loop early - because there is no other way to do this.

    • This is nice because it allows us to provide two ways for a for loop to end - either it loops through everything, or it breaks due to some condition.
  • In general, you should avoid using break in a while loop, because it is an indicator that you could just write a better condition for the loop.

    • Take the first example above as an example of BAD break usage. Instead of using a conditional to break out of the loop, you could’ve rewritten line 3 to while x >= 3:. This makes it easier to read and understand the purpose of the loop, and it eliminates the need of the break keyword.
Next Section - Lesson 03-11: for-else Loops