Break-Statement-In-Python .

In this session i have discussed break statement in python .


The break is a keyword in Python which is used to break the loop . If you will use the break in inner loop then it will terminate the inner loop only .

Example : Write a Python program that will demonstrate the break statement in loop .

i=1
for i in range(1,6):
    print(i)
    if(i==3):
        break
print("Out of loop %d"%(i))
 
    OUTPUT : 1 
             2
             3 
             Out of loop i=3
    

Example : Write a python program that will demonstrate the break statement in nested loop .

i=0
j=0
for i in range(1,4):
        
    for j in range(1,4):
            
        print(i,j)
        if(i==2 and j==2):
                
            break #will break inner loop  only
    
    OUTPUT : 1 1
             1 2
             1 3
             2 1
             2 2
             3 1 
             3 2
             3 3
    


Welcome to AndroidATPoint.

back image                     next image