This post is about the Python keyword break, not to mention continue and pass
break
This causes a break in the computer action. Whatever the program was doing, it stops doing it. It’s like when a cop on a bad television show yells, “Freeze!” This is great if you want your function to stop looping for some reason. In the code below, for example, the code stops as soon as the name of “Rudolph” is encountered. After all, some Christmas legend purists will say that Rudolph is a wanna-be reindeer made up by Robert May as recently as the 20th century.
#this code uses "break"
santasReindeer = ["Dasher", "Dancer", "Prancer", "Vixen", "Rudolph", "Comet", "Cupid", "Donner", "Blitzen"]
for deer in santasReindeer: if deer == "Rudolph": break print(deer) #Run this code and you'll get the following: Dasher Dancer Prancer Vixen
continue
Technically, the keyword class should come next, but we’re jumping ahead to continue because it can be an alternative to break in your code. That is, instead of stopping the code midstream, continue will allow it to continue after it eliminates whatever seems to be troublesome or unwanted. To see what I mean, let’s go back to Santa’s reindeer:
#this code uses "continue"
santasReindeer = ["Dasher", "Dancer", "Prancer", "Vixen", "Rudolph", "Comet", "Cupid", "Donner", "Blitzen"]
for deer in santasReindeer: if deer == "Rudolph": continue print(deer) #Run this code and you'll get the following: Dasher Dancer Prancer Vixen Comet Cupid Donner Blitzen
This time, all the reindeer except Rudolph are printed out. You’ve got to feel a bit sorry for Rudy. After all, we’re using code to exclude him from joining any reindeer games (like Monopoly).
pass
Let’s also have a look at pass so that we’re grouping similar keywords together. Pass is the term you use when, syntactically speaking, you’ve got to do something but you don’t really want to do anything. For example, maybe your code requires you to use an if statement somewhere but you don’t particularly want to do anything useful with that statement. In that case, pass can be handy. Here’s a way we can use it in our reindeer code.
#this code uses "pass"
santasReindeer = ["Dasher", "Dancer", "Prancer", "Vixen", "Rudolph", "Comet", "Cupid", "Donner", "Blitzen"]
for deer in santasReindeer: if deer == "Rudolph": pass print ('"My deers, stop excluding Rudy from all your reindeer games."') print(deer) print('Dasher responding: "Okay, Mrs. Claus. Sorry!"') print('Dasher whispering to Rudolph as an aside: "Come on, you red-nosed freak. Just wait till we play Kill the Carrier. Yur going to get it!"')
# Here's how it should print:
Dasher Dancer Prancer Vixen "My deers, stop excluding Rudy from all your reindeer games." Rudolph Comet Cupid Donner Blitzen Dasher responding: "Okay, Mrs. Claus. Sorry!" Dasher whispering to Rudolph as an aside: "Come on, you red-nosed freak. Just wait till we play Kill the Carrier. Yur going to get it!"
Yes, kids can be cruel. But don’t worry, Rudolph wins them all over eventually. He even winds up going steady with Vixen, yet another nerd made good.
Hermey the elf and Rudolph, two characters from Rudolph the Red-Nosed Reindeer. See https://en.wikipedia.org/wiki/ Rudolph_the_Red-Nosed_Reindeer#/media/ File:Hermey_the_elf_and_Rudolph.jpg