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.
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")
while(1): print(i) if i==6: print("We break while loop at i=6") break i=i+1
print("While loop terminate")
Break in for loop
At i==5 be break the for loop
Break in while loop
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.
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!