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:
This also works for for loops as well:
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
breakto exit aforloop 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
breakin awhileloop, 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
breakusage. Instead of using a conditional to break out of the loop, you could’ve rewritten line 3 towhile x >= 3:. This makes it easier to read and understand the purpose of the loop, and it eliminates the need of thebreakkeyword.
- Take the first example above as an example of BAD
for-else Loops