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.