Using Break and Continue in Python

Sovary June 10, 2022 415
3 minutes read

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.

python break continue

Break Statement

"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.


Line 3: if we found tomato which is vegetable then we want to stop the loop by using break statement.
Line 6: after exit the loop, the below loop content still work and print out as "Hello"

Using Break in Nested Loop

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.

Continues 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

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.

Python 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him