Break vs Continue: Which One Should You Use?

Plus a video course, interesting packages, and articles.

šŸ‘‹ Hey there!

This weekā€™s snack is a bit longer than previous weekā€™s. This is a topic Iā€™ve frequently found myself explaining to my students.

So I figured ā€œwhy not, letā€™s make a snack out of itā€.

Hereā€™s whatā€™s in store for this newsletter:

  • The difference between the continue and break statements,

  • A different kind of coding challenge

  • And much moreā€¦

Forwarded this email? Sign up here for free.

šŸ„ The Python Pantry

Curated links to help you become a better programmer

šŸŽ’ Tutorials
- Using the ChatGPT API in Python (link)
- The definitive guide to Python virtual environments with Conda (link)
- How to process images with the Pillow library [Video Course] (link)

šŸ“¦ļø Packages
- Cerberus: Data validation for Python (link)
- Salmon: a pure Python mail server (link)
- Gymnasium: Develop and compare reinforcement algorithms for machine learning (link)

šŸ’°ļø Job Postings
- Amazon is looking for a Systems Development Engineer for Software Health Automation (link)
- Beckman Coulter Diagnostics is looking for a Senior Staff Software Architect ā€“ Integrated Software Platform (link)

šŸ“• Articles
- DjangoCon Europe 2024 early bird tickets are available (link)
- 10 Things Software Engineers Should Learn About Learning (link)

šŸ’” This Weekā€™s Snack

The weekly tip/trick and coding challenge

In loops, youā€™ll sometimes encounter the continue and break statements. What do these statements mean and when do we use them?

These statements are used to help control the behavior of the loop iteration. Theyā€™re commonly used when we are looking to satisfy a condition, such as identifying the first instance of a certain value.

TL;DR, hereā€™s the difference:

Ā» break is a statement that allows us to exit out of the loop early.
Ā» continue is a statement that lets us skip to the next iteration of the loop.

The big question you should have in mind for this: Do I care about the next iteration in this loop?

If the answer is yes, then youā€™d likely want to use continue. If not, then use break.

Letā€™s look at the behavior differences with some code snippets. Here, Iā€™ll instantiate a list of 1 to 5 and see if the number is 3. Each iteration, weā€™ll let the user know whether if the number is 3 or not.

First, letā€™s look at the behavior of when we use continue:

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 ran, weā€™ll get 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!

But if we use break instead of continue in the code snippet above, weā€™ll get the following output:

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

Notice that the number 4 and 5 werenā€™t printed: this is because we exited out of the loop early.

Give this weekā€™s challenge a shot if you think you understand. Instead of code writing, see if you can explain what is going on:

def break_or_continue(numbers):
  for i, number in enumerate(numbers):
    if number % 3 == 0:
      continue
    elif i % number == 0:
      break
   
   print(number)

Explain how the following output happens when the function is called with the following list:

>>> break_or_continue([15, 22, 29, 3, 18, 9, 27, 11, 14])
22
29
11
14

šŸ“§ 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.