Python: Break And Continue Statement


In this article, we will discuss break and continue statements. Break and continue statements also known as jumping statements. Sometimes we need to skip some iterations or maybe we need to stop our iteration, then we use a break and continue statements.
Break statement
It allows us to exit the loop at any point in condition. When a break statement encountered inside the loop then our loop terminates and our control jump out from the loop. For example, there is a list of numbers and you want to search a particular number inside the list, then you will start from the beginning and check each and every number with your search element, and if the search number found inside the list then you will never check further and break the loop.
  1. break;   
Example 1:
  1. Names=["Sourabh Somani","DJ","Dinesh Beniwal","Abhishek Jaiswal"]    
  2. Key=input("Please Enter the name which you want to search : ")    
  3. i=1;    
  4. for name in Names:    
  5.     if(Key==name):    
  6.         print(name," is found at ",i," Position in the List")    
  7.         break    
  8.     i+=1;    
  9. else:    
  10.     print("Name is not present in the list"
Example 2
  1. #program to check         
  2. #weather the given number is         
  3. #prime number or not        
  4. num = int(input("Enter a Number : "))          
  5. for i in range(2,num):        
  6.     if(num%i==0):        
  7.         print("%d is not a prime number..."%num)        
  8.         break        
  9. else:        
  10.     print("%d is a prime number..."%num)  
Example 3: 
  1. while(True):  
  2.     a=int(input("Please Enter 1st number :"))  
  3.     b=int(input("Please Enter 2nd number :"))  
  4.     print()  
  5.     print("MENU")  
  6.     print("_____________________")  
  7.     print("PRESS 1 FOR ADD")  
  8.     print("PRESS 2 FOR SUBTRACT")  
  9.     print("PRESS 3 FOR MULTIPLICATE")  
  10.     print("PRESS 4 FOR DIVIDE")  
  11.     print("PRESS 5 FOR EXIT")  
  12.     print("_____________________")  
  13.     print()  
  14.     choice=int(input("Enter your choice : "))  
  15.     if(choice==1):  
  16.         print("Sum of %d and %d is %d"%(a,b,a+b))  
  17.     elif(choice==2):  
  18.         print("Subtraction of %d and %d is %d"%(a,b,a-b))  
  19.     elif(choice==3):  
  20.         print("multiplication of %d and %d is %d"%(a,b,a*b))  
  21.     elif(choice==4):  
  22.         print("Division of %d and %d is %d"%(a,b,a/b))  
  23.     elif(choice==5):  
  24.         print("Bye Bye")  
  25.         break;  
  26.     else:  
  27.         print("Invalid Choice");  
  28.     print()  
Continue is used when you want to skip certain iterations of the loop. This means it suspend the execution of the loop for that iteration and transfer control back to the loop for the next iteration. For example, suppose you want to print numbers from 1-30, but you don't want to print multiples of the 5, then you will execute your loop from 1 to 30, but you will skip the loop where the number will be divided by 5.
  1. continue  
Example 1: 
  1. for i in range(1,31):  
  2.     if(i%5==0):  
  3.         continue  
  4.     print(i)  
Example 2:
  1. #This Programs     
  2. #removes vowels     
  3. #from the string    
  5. name=input("Please Enter a String : ")    
  6. new_str=""    
  7. for ch in name:    
  8.     if(ch=='A' or ch=='E' or ch=='I' or ch=='O'or ch=='U'  or ch=='a' or ch=='e' or ch=='i' or ch=='o' or ch=='u'):    
  9.         continue    
  10.     new_str+=ch    
  11. print(new_str)

Similar Articles