• Python Snacks
  • Posts
  • The difference between Break vs Continue in Python

The difference between Break vs Continue in Python

When working with loops in Python, two keywords you’ll frequently come across are break and continue.

These statements let you control the flow of your loops in a more precise way. But what exactly do they do, and when should you use one over the other?

In a nutshell:

  • break: Exits the loop completely.

  • continue: Skips the current iteration and moves to the next one.

So, how do you decide between break and continue? It comes down to one key question: Do you want to continue with the next iteration?

  • If yes, use continue.

  • If not, use break.

Let’s dig into these concepts with some examples to clarify.

Using continue in a loop

The continue statement is helpful when you want to skip certain conditions but keep looping through the rest of the elements.

Say you have a list of numbers from 1 to 5, and you want to skip printing the number 3 but continue with the rest of the loop:

my_list = [1, 2, 3, 4, 5]

for number in my_list:
    if number == 3:
        print(f"Number found: {number}")
        continue
    
    print(f"Number not found (current: {number})")

print("Loop completed!")

When this code is run, it will print the following output:

Number not found (current: 1)
Number not found (current: 2)
Number found: 3
Number not found (current: 4)
Number not found (current: 5)
Loop completed!

In this example, when the loop encounters number == 3, it prints “Number found: 3” and skips the rest of that iteration, skipping over the second print statement.

Using break in a Loop

The break statement, on the other hand, will exit the loop entirely. This is useful when you’ve found what you’re looking for and don’t need to continue checking the rest of the elements.

Let’s modify the code above to break out of the loop once we find the number 3:

my_list = [1, 2, 3, 4, 5]

for number in my_list:
    if number == 3:
        print(f"Number found: {number}")
        break
    
    print(f"Number not found (current: {number})")

print("Loop completed!")

As soon as the loop encounters number == 3, the break statement exits the loop, so numbers 4 and 5 aren’t printed.

The loop finishes as soon as the condition is met, saving time by avoiding unnecessary iterations.

Summary of Break vs Continue

In short:

  • Use break when you want to completely exit a loop once a condition is met.

  • Use continue when you want to skip the current iteration but keep looping through the rest.

Understanding the difference between break and continue helps you manage loop flow more effectively and can make your code cleaner and more efficient.

📧 Join the Python Snacks Newsletter! 🐍

Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:

  1. Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.

  2. Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.

  3. Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.

You can unsubscribe at any time.

Interested in starting a newsletter or a blog?

Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.

If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.

This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.

Reply

or to participate.