Learn Python Programming

The break and continue Statement


The break statement can be used inside a for loop or while loop in order to come out of a loop. When ‘break’ statement is executed, the python interpreter jumps out of the loop to process the next statement in the program. However the loop condition yet true, the break statement break the loop and proceed to next statement.

Example: break statement

A Python program to display numbers from 10 to 6 using break statement

x=10
while x>=1:
		print('x= ', x)
		x-=1
		if x == 5:
				break
print('Out of loop')

#Output
x= 10
x= 9
x= 8
x= 7
x= 6
Out of loop

The continue Statement

The continue statement is used in loop to go back to the beginning of the loop and execute the next round of loop and skip the current. When the continue statement is executed, the subsequent statement in the loop are not executed.

Example: continue statement

A Python program to display numbers from 1 to 5 using continue statement

x=10
while x>=1:
		print('x= ', x)
		x-=1
		if x == 5:
				break
print('Out of loop')

#Output
x= 10
x= 9
x= 8
x= 7
x= 6
Out of loop