Guide to Continue and Break in Python Loops
So far in this section on Python loops each time that we've implemented a for-in loop the loop has gone from the beginning of the collection all the way to the end and in many cases that is exactly the behavior that you would want.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
  • Complete the Exercise
Video locked
This video is viewable to users with a Bottega Bootcamp license

However, there are some times where you actually want to stop or alter the behavior somewhere in the middle of the loop based on a condition. So for example I have right here a list of user names so we have these five usernames and they are each represented as strings in this user names variable.

usernames = [
  'jon',
  'tyrion',
  'theon',
  'cersei',
  'sansa',
]

So what happens if we want to build a loop that searches for a name and then if it finds the name it will actually alter its behavior? Well, we have two different types of control flow logic operators. One is called Continue and then the other is called Break. And so these are the two different flow operators that we are going to work through in this guide.

For the first loop let's say for username in usernames. So far this is exactly what we talked about from the beginning of this section are simply implementing a basic for in loop but now it's going to get a little bit trickier because what I want to do is I want to search for this name Cersei here and if it finds Cersei I want there to be a different output than if it is anybody else. And so imagine a scenario where you're building now some kind of web or mobile application and you have a list of blacklisted individuals and so these could be banned users. Anything like that and you want to be able to know who they are, well this is how you can do it

So down inside of the loop block, I can say if username so right here I am referencing each one of the values. So the very first time it loops it's gonna look for jon then it's going to look for tyrion so on and so forth. So I mean to say if username and then this is very important. We're going to have an entire section on conditionals so I don't want you to get too hung up if this looks a little odd but we're going to give a double equal sign so I'm gonna say If username is equal to cersei and give another colon here at the end.

So this is going to be a secondary and nested code block inside of this if statement. If the username is equal to cersei then I want to do a print statement and I'm going to format it as well and so I'm gonna say f and then sorry and I'm gonna use the curly braces. So you say whatever the username is you are not allowed and then end the quotation mark. And this is the key right here.

This next line we're going to say continue. And if this looks a little bit weird don't worry after I run the code you're going to see exactly what this is doing. And then you say else followed by a colon and then if it is the if it falls into this second condition I'm going to use the format once again and then start with the curly brace say username is allowed.

And that is all we need to do to get this working. So we right here are learning what continue represents so if I run this code here you can see the first time it loops through it says jon as allowed. Then it says tyrion is allowed. Then it says theon is allowed. But notice what happens when it gets to cersei. It says Sorry, cersei you are not allowed, and very importantly it follows and it continues hence the continue keyword and it says sansa is allowed.

large

and so remember at the beginning I said we're going to talk about to control flow operators. One is to continue the other is break.

So what continue does when you place it inside of a loop like this is if it finds this kind of condition. So if this is true it finds a username that is equal to cersei it's going to change its behavior. But what continue does is it tells the program to keep on going through the loop. So it's going to continue to iterate and it is not going to stop once it finds what it's looking for. Now that may sound like it's really not a big deal because technically this is the exact behavior that you would expect because it is looping through all of the usernames and the part of the reason why I wanted to show you this is because this is the exact opposite behavior compared with if we used break and so that is the second example.

I'll put all of this in the show notes so you can have access to all of it but in our second example, the behavior I want is more of a search and destroy kind of mission where I want to look through this list of usernames and if I find the one that I'm looking for I don't care about any of the other ones after I have found my condition.

So, in this case, I'm going to get rid of all of these. Then inside of this block I'm just going to say because I want to show you some other access points I'm going to say print f quotation mark because we're formatting once again and then say username was found at index and I can call the usernames collection here and then call our index function. If you remember that back from when we went through the list section and say index username. So I'm essentially just doing a search right here and then end the curly brace and the quotation mark and then the print statement.

for username in usernames:
  if username == 'cersei':
    print(f'{username} was found at index {usernames.index (username)}')

We have our print statement here. Then we have our single quotation mark and then we have two sets of curly brackets one here with username and the other one here that says usernames dot index and then we're searching for the username right here that is cersei then I'm going to on the same line as our print statement. So notice how we are still nested inside of this if conditional.

Now I'm going to say break and then down below. And notice here when I type break, automatically I was taken down and I was taken outside of this if statement because anything after a break is not going to happen if it is here and it's nested inside of this code block

large

Next, I'm just going to say print username, and let's see what happens here, and let's notice how much different the behavior was when we use break versus continue. So if I print this out you can see

large

it starts off like normal prints out jon, tyrion, theon and then it prints out exactly what you'd expect here when it finds Searcy. So it says cersei was found at index 3 which remember the whole point of this little program was to find and let us know exactly where we found our condition here cersei but the big key to notice here is that sansa is not included.

So that is the key difference between break and continue, with continue the program simply keeps going even though it found what it was looking for. It still goes through the entire collection which is many times exactly what you want it to do. However, there are also times where you don't want to go through an entire collection because it would be a waste of resources. You simply want to search and then once you have found what you're looking for then you want the program to stop. And that's exactly what break does.

Break not only breaks you out of this conditional it looks all the way up top so it traverses down this chain and it looks and it notices that it is in a for-in loop and it tells Python OK we found what we want and we now want to break out of the loop and python stops and that's a reason why any other elements after cersei right here will not get run.

Code

usernames = [
  'jon',
  'tyrion',
  'theon',
  'cersei',
  'sansa',
]

for username in usernames:
  if username == 'cersei':
    print(f'Sorry, {username}, you are not allowed')
    continue
  else:
    print(f'{username} is allowed')

for username in usernames:
  if username == 'cersei':
    print(f'{username} was found at index {usernames.index(username)}')
    break
  print(username)