Python Break

Python provides the break statement to implement the middle-existing control logic, if you need to exit a loop without waiting for the normal termination.

The break statement causes the immediate exit from the body of the loop. If we execute a break statement inside a for or while loop, control passes out of the loop.


Syntax of Break Statement

 
for variable in sequence condition: break statements


Example of break statement in for loop

>>> for n in range(1, 10):
	if(n == 5):
		break
	print(n)

	
1
2
3
4

The break statement is the only the way to get out of the loop.





Example of break statement in While loop

>>> i = 0
>>> while i < 90:
	i = i + 3
	if(i % 7) == 0:
		break
	print i,

	
3 6 9 12 15 18