Join Barron Stone for an in-depth discussion in this video Break statements, part of Programming Foundations: Real-World Examples.
- Now that all of my dishes are finally clean, I'm ready to unload the dishwasher and put the clean dishes into the cabinet. This is a perfect operation to execute using a for loop, because I know that there are 20 clean dishes in the dishwasher, and I plan to unload all of them. So, I'll count aloud as I unload each one. I'll pick up the first dish, put it away. That's one. I'll pick up the second dish, and put it away as well. That's two. I'll pick up the third dish.
Well, my cabinet is full, which means I need to stop this process, because there's no space left. I had originally planned to put away all 20 dishes, but because this happened, I'll need to exit my for loop early, and leave the unprocessed dishes in the dishwasher. If I had checked the cabinet before I started, I might have seen that it only had space for two dishes, which is way less than the number of dishes in the dishwasher, and in that case, I could have modified my for loop to only process two dishes instead of trying to process all 20. But if I can't look at the cabinet ahead of time to determine how much space it has, or maybe I'm just really bad at estimating space, then I'll start unloading dishes as a for loop, because I want to unload them all, but if that unpredictable condition occurs, then I'll need to exit the loop to quit early.
Fortunately, we can escape for loops early using the break statement. This script from the exercise files called start_10_03_for_break represents the process of putting away the clean dishes into the cabinet. I've created a list of 20 dishes representing the 20 dishes that are in the dishwasher. Then I use a for loop to iterate through a copy of that list of dishes, so I can put them away into the cabinet. In each iteration of the loop, I use the randint function from the random module, to determine whether or not there is still space available in the cabinet.
I'm generating random numbers between 0 and 19 so that every time I check the cabinet, there will be a 1 in 20 chance that it's full. If the randint function returns a non 0 number, then that means there's still space in the cabinet, so I print out that I'm putting away the dish, and I remove it from the original dishwasher list. If the randint function returns 0, then that means the cabinet's full, so I print out a message that I'm out of space, and I use the break statement to exit the for loop. Whenever I call break inside of a for or while loop, the program will exit the loop at that point, and will continue executing the next thing in my program after the loop.
In this case, there's nothing after the loop, so the program will finish. Now, if I run the script, you'll see that i was able to put away all of the dishes before the cabinet became full. If I run the script again, this time I was only able to put away 13 of the dishes before the cabinet became full. You might be thinking that this randomness seems kind of like the situation where you'd want to use a while loop, because you don't know how many times the loop will execute before the program runs. That would be one way to write this program. It would be possible to implement this routine with a while loop by checking for the condition that either the cabinet is out of space, or that we've put away all the dishes in the dishwasher.
But, there's still a reason that it makes sense to use a for loop for this program, and that's because we're iterating through objects in a list. If we have good luck, and the cabinet remains empty the whole time, then the for loop will execute, and put away all 20 of their dishes in the dishwasher. That's great. That's what we want to happen, so it makes sense for us to use a for loop for this job. We have the break statement in it, because there is the potential that our loop might not be able to process all the dishes as we originally planned, and if that happens, the break statement gives us an escape route from the loop.
- Reusing functions
- Local vs. global variables
- Creating and naming custom objects
- Class inheritance
- Modules and packages
- Multidimensional lists and tuples
- Queues and stacks
- Creating and combining sets
- Storing data in dictionaries
- If/else and switch statements
- For vs. while loops
- Error handling
- Polling and event-driven programming