Today we will learn how to use continue
and break
in loop. I will explain the way to exit loop and bypass the statement with break
and continue
statement. break
and continue
statement are use inside loop (for loop, while loop) to interrupted or skip block of code.
"break statement" is used to stop the loop when compiler reach that statement. After terminate the loop the statement, the below content loop are still executed. In Python break
is keyword to be used in this case.
Example #1 - Using break with for loop
fruit = ["apple","mango","tomato","bread"]
for i in range(len(fruit)):
if fruit[i] == "tomato":
break
print(fruit[i])
print("Hello")
Output #1:
apple
mango
Hello
Explain #1:
Line 1: create list of fruit, this contain 2 fruit and 2 vegetable and we will loop each item in the next step.
Line 2: we have a list of fruite and we loop through one by one from apple as first element.
break
statement.Using break in nested loop will exit the loop which is the most inner loop.
Example #1:
# first loop
for i in range(2):
#second loop
for j in range(4):
# if check j greater than 2 and exit second loop
if j > 2:
break
print("i=",i,"j=",j)
Output #1:
i= 0 j= 0
i= 0 j= 1
i= 0 j= 2
i= 1 j= 0
i= 1 j= 1
i= 1 j= 2
Explaination #1:
Create at first loop with range 0-1 which is will loop 2 times but at first time loop we fall into second loop with range 0-4 which is will loop 4 times.
At first time loop i variable hold 0 and second loop j hold 0 and we check if j greater then 2 exit the loop with break
statement but j not increase to 2 yet so it's going to print out "i=0 j=0"
Not fall back to first loop yer the second loop increase j to 1 until second loop increase j and check if it greater than 2 then exit the second loop which is nearest loop with break
statement.
Using continue in loop will skip the below part of code to executed and continue to loop cycle. The continue
keyword have been use in Python to skip running the code.
Example #1 - Using continue with while loop
i=0
while i < 5 :
i+=1
if i == 3:
continue
print(i)
Output #1:
1
2
4
5
Explaination #1
Line 1: Define i variable to zero and start while loop with increment i by 1 at Line 3.
Line 3: Once i in loop increase to 3 as we check in Line 4 to skip up without print i which is below continue statment. The loop keep running until false condition.
Line 6: As output we never see i print number 3
Using continue in nested loop will exit the loop which is the most inner loop.
Example #1:
i=0
# first loop
while i < 5 :
i+=1
# reset j
j=0
# second loop
while j <2 :
j+=1
if i == 3:
continue
print("i=",i,"j=",j)
Output #1:
i= 1 j= 1
i= 1 j= 2
i= 2 j= 1
i= 2 j= 2
i= 4 j= 1
i= 4 j= 2
i= 5 j= 1
i= 5 j= 2
Explaination #1:
Line 1-4: initialize value i to 0 then fall into first loop and start increase i to 1
Line 6: reset j to value 0 each time first loop repeated and fall into second loop which is increase j to 1. The first and second loop repeate code again and again and print to console.
Line 10: until first loop increase i to 3, the condition i == 3 evaluate true the code below continue
statement start skip the part to print i =3
Hope you learn and help you to understand basic how to using break
and continue
statement in Python.
As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him