Introduction to python break and continue statements:
In last few articles we have learned about looping in python and we have covered topics python for loop and python while loop. Basically these looping statements in python allows us to repeat the same task or code block efficiently. Using looping statements we can repeat the execution of code until the specified condition is satisfied.
However sometimes we may come across some situations wherein:
- We want to terminate the loop execution and exit the loop without checking the test condition.
- Or skip some part of loop code execution for few iterations.
While execution of python loop, There are some external factors which needs to considered. These factors impacts the loop execution. Like sometimes depending upon these factors, We have to take the decision whether to continue or exit the loop. Therefore these external factors can be used as a triggers to continue or exit the loop.
To react with the external triggers and decide whether to continue or exit the loop execution, There are two statements available in python.
These statements can change the normal flow of loop execution. Let’s see each of these statement in detail.
Python break statement:
Break statement in python terminates the loop execution without checking the test condition. As said earlier, Sometimes we may want to exit the loop when the external condition is triggered. To do that break statement is used. It terminates the same loop within which it is present.
Python break statement is written after conditional “if” statement within loop. During execution of loop statements it checks the if condition. The time when if condition evaluates to “True”, “break” statement considers it as a trigger and terminates the loop execution.
Flowchart of python break statement:
Break statement in for loop and while loop:
Python break statement is used within both for loop and while loop. Images below explains about how break statement behaves within both of these loops.
- Break statement within for loop:
- Break statement within while loop:
Examples of break statements in while loop:
Example 1: Break statement in while loop. (Python program to print only three digit number.)
In this program we’ll ask the user to enter a number. We are going to print until the user enter the three digit number. As soon as the user enters the number other than three digit we are going to exit the loop.
Input_Number = 0 while int(Input_Number) < 999: Input_Number = input("Please enter a number to print:") if int(Input_Number) < 100: break print("You have entered:", Input_Number) print("Sorry, The number entered is not a three digit number.") #Result: Please enter a number to print:172 You have entered: 172 Please enter a number to print:99 Sorry, The number entered is not a three digit number.
In above example we have initiated the while loop with the condition that if user input is less than 999 then keep on iterating while block.
In while loop we have added one conditional if statement that is, if the user input number is less than 100 the break the loop execution.
Example 2: Break statement in for loop. (Python program to print letters of word till specific letter in the word.)
In this sample program we are going to ask user to enter any word. We are going to print each letter of word but if this word contains letter “i” then we are going to terminate the for loop execution.
Name = input("Please enter any word:") for var in Name: print(var) if var == "a": break #Result: M i c h a
Python continue statement:
Continue statement in python is used to skip the execution of rest of the loop code for a single iteration. This statement do not terminate the loop execution but only skips the execution of some part of loop code. This skipping of code execution occurs only for single iteration.
In python continue statement is written inside the loop and below conditional if statement. The time when if condition evaluates to “True”, continue statement recognizes it as a trigger to skip the execution of remaining loop body.
Python continue statement is used along with for loop and while loop.
Flowchart of python continue statement:
Difference between break and continue statement:
If you see the syntax difference between break and continue statements then, There is no major difference. Only the break word is replaced by the continue. However there is major functional difference between these two statements.
- Break statement entirely terminates the loop execution.
- Continue statement only skips the code execution and that too for single iteration.
Continue statement in for loop and while loop:
In python continue statement can be used within both for loop and while loop. Images below explains about how continue statement behaves within both of these loops.
- Continue statement within for loop:
Examples of continue statements in while loop:
Example 1: Continue statement in for loop: (Python program for printing letters of word, except specific letter.)
In this program, We’ll be asking the user to input a word. Print all the letters present in the word except letter “e”. We are not going to print letter “e” from the input word.
Name = input("Please enter any word:") for var in Name: if var == "e": continue print(var) #Result: Please enter any word:hello h l l o
Example 2: Continue statement in while loop: (python program for printing the odd numbers.)
In this example, We are going to ask the user to input any number (User_Number). Then we are going to print all the odd numbers between zero till the user entered number.
User_Number = input("Please enter a number:") Internal_Number = 0 while Internal_Number != int(User_Number): Internal_Number = Internal_Number + 1 if Internal_Number % 2 == 0: continue print(Internal_Number) #Result: Please enter a number:10 1 3 5 7 9
In above example, We have initiated the while loop and also one variable “Internal_Number”. Initial value of “Internal_Number” is zero and we increment it by one after each loop iteration. Also after each iteration we are going to print value of variable “Internal_Number”.
So it will print all the numbers starting from zero till the user entered number. However we are only interested in printing the odd numbers.
Therefor we have inserted the conditional if statement along with continue statement. Here if condition checks whether the number is a even number. If it is even number then continue statement skips the print line of while loop.