You are here: Home / Topics / What is Break statement in python

What is Break statement in python

Filed under: Python on 2022-08-31 16:25:55

There are two type of loop in python -
- for loop
- while loop

-At the time of execution of loop, if we want to terminate the loop before its actual termination point then we use break inside loop or we can say break is used to terminate the loop and resume the next instruction in the program.
- break is only used inside the loop otherwise it will generate the error.

 

program -

print("Break in for loop")
for i in range(100):
print(i)
if i==5:
print("At i==5 be break the for loop")
break

print("Break in while loop")
i=1
while(1):
print(i)
if i==6:
print("We break while loop at i=6")
break
i=i+1
print("While loop terminate")
 


output -

Break in for loop
0
1
2
3
4
5
At i==5 be break the for loop
Break in while loop
1
2
3
4
5
6
We break while loop at i=6
While loop terminate
 


- In this program we simply show the concept of the break statement by python programming language. In while loop we pass 1 as a condition so it will goes infinite but here we use break , so when i become 6 break statement execute and loop terminated.

About Author:
Y
Yogesh     View Profile
I am Yogesh Sharma. I found this website very useful for all the students. It is a very good website for learning thousands of multiple choice questions. Basically it is like a community website. Everyone can add questions so am I. I will add mcqs in this website to help you.