- Python Snacks
- Posts
- Break vs Continue: Which One Should You Use?
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
Want even more Python-related content thatās useful? Hereās 3 reasons why you should subscribe the Python Snacks newsletter:
Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.
Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.
Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.
You can unsubscribe at any time.
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