Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Using Break and Continue

In this lesson you’ll see practical examples of the two loop interruption options you’ve learned about in the last lesson. The used examples are debugged in the video, so that you get a sense of what’s happening behind the scenes. The examples covered in this lesson are displayed below.

Python
n = 5
while n > 0:
    n = n - 1
    if n == 2:
        break
    print(n)
print("Loop is finished")
Python
n = 5
while n > 0:
    n = n - 1
    if n == 2:
        continue
    print(n)
print("Loop is finished")

If you’d like to explore another use case for the break statement, then check out How Can You Emulate Do-While Loops in Python? from Real Python.

00:00 All right, so let’s see how we can use these key terms. I’m going to write another while loop that was pretty similar to the original one we saw.

00:10 First, we need to initialize a variable and say n = 5. Then I’m going to write my while loop. And my condition is going to be if it’s greater than 0, then I’m going to execute the block of code below.

00:27 So, first thing we did was decrement n by 1. And now this is where I’m going to include a conditional statement, my if statement.

00:39 if n == 2: I’m actually going to break out of my block. But if it does not equal 2, then I’m going to continue on and I’m going to print n just like before.

00:59 Now, this next piece right here is going to execute once the while loop is finished normally. I’m just going to print 'Loop is finished'.

01:16 All right. So, take a minute to think about what is going to happen and then we’ll run it and see if you’re right. All right, let’s see. All right, so what I see now is that I printed those first two like I did before. I have 4 and I have 3. But whenever n Over here we can see n is currently equal to 2, it ran into this if statement and it actually broke out of that code entirely and it went down to the first statement after the while loop, which is 'Loop is finished'.

01:51 So now I’m going to simply change this word break to continue.

02:06 Now let’s see what happens when I run this code with the word continue. All right, so we see now I have 4 and 3 still. I don’t have 2, but I did continue on and have 1 and 0.

02:22 And then once that loop was exhausted, I then printed my Loop is finished. Let’s look at this step-by-step with our debugger.

02:39 So first, we’re going to initialize our variable to 5, and I’m going to do just like I did before. 5 is greater than 0, so it returns True.

02:52 4 does not equal 2, so this returns False, so it’s not going to execute that code but it is going to go ahead and jump to the next line, which is to print n.

03:02 But this is still inside of my loop, so I’m going to then jump back up to the top of my loop and check if the condition is still True.

03:20 I’ll continue to do this until I get to 2. Now when n equals 2, this is going to return True. And if this returns True, my if statement is going to start. So if this returns True, I’m going to jump into my if statement and it says continue, so what’s going to happen is I actually jump back up to my while loop and I check my condition.

03:48 And here, I see that it’s still True, so I’m going to continue on.

03:58 And that’s why I get the 1 next and I left out 2. It was because it never actually reached the print(n).

Avatar image for phalimem

phalimem on March 22, 2020

Hi

How do I create a program where I ask a user to enter a number if the number is not equal 10 then the loop must stop when they enter 10 and add all the numbers the user entered before they enter 10

Avatar image for Ricky White

Ricky White RP Team on March 23, 2020

Hi phalimem. Be sure to watch the other basic courses to get a grasp on all the different conditional statements available in Python. That will help you find the answer to your question.

realpython.com/python-conditional-statements/

Become a Member to join the conversation.