Introduction
Welcome to my 19th blog post. Today I learned about 'break' and 'continue' statements in python. Now let's dive deep into the details
So let's get started......
Break Statement
While using any kind of loop, sometimes we want to break the flow of the loop at a particular iteration, then, in that case, we use the 'break' statement and in case we want to skip a particular iteration and work on the next iteration of any loop then we use the 'continue' statement.
Note - In break statement we expect to move out of loop, whereas in continue statement we expect to move out of an iteration and continue to next iteration.
Example -
Normal Code:
for i in range(13):
print("5 X",i, "=", 5*i)
Output
5 X 0 = 0
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
5 X 11 = 55
5 X 12 = 60
Example -
Using Break Statement -
#using break
print("\nusing break")
for i in range(13):
if (i==10):
print("moving out at 10")
break
print("5 X",i, "=", 5*i)
print("Moved out of loop \n")
Output -
using break
5 X 0 = 0
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
moving out at 10
Moved out of loop
Some more example
for i in range(1,101,1):
print(i ,end=" ")
if(i==50):
break
else:
print("Mississippi")
print("Thank you")
Continue Statement
The continue Statement skips the iteration and causes the next iteration to occur
Example -
#using continue
for i in range(13):
if (i==10):
print("Skip iteration at i = 10 ")
continue
print("5 X",i, "=", 5*i)
Output
5 X 0 = 0
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
Skip iteration at i = 10
5 X 11 = 55
5 X 12 = 60
Some more code -
for i in [2,3,4,6,8,0]:
if (i%2!=0):
continue
print(i)
2
4
6
8
0
Complete Code for reference -
for i in range(13):
print("5 X",i, "=", 5*i)
print("\nusing break")
#using break
for i in range(13):
if (i==10):
print("moving out at 10")
break
print("5 X",i, "=", 5*i)
print("Moved out of loop \n")
#using continue
for i in range(13):
if (i==10):
print("Skip iteration at i = 10 ")
continue
print("5 X",i, "=", 5*i)
#more example
for i in range(1,101,1):
print(i ,end=" ")
if(i==50):
break
else:
print("Mississippi")
print("Thank you")
Resources Used
You can watch the video of Day#19 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. Break and Continue statements play a major role in executing the relevant code in programming
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of python coding. This is it for Day #19
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practising coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.