Python break statement

In this tutorial, we will learn about the break statement and working examples of it along with the use cases of break statement.

break statement

The break statement can be used inside a for loop or while loop to come out of the loop. When 'break' is executed, the Python interpreter jumps out of the loop to process the next statement in the program.

Suppose, we want to display numbers from 10 to 1 in descending order using while loop. For this purpose, we can write a simple while loop as:

break statement examples:

CopiedCopy Code

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

If we want to break the loop execution on certain condition, then break statement will be useful.

break statement:

CopiedCopy Code

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