Today we will learn about the Python break and continue statements. These Python keywords are used to change the flow of a loop in Python.
In the previous post, we talk about the Python for loop where we discuss how the flow of the loop can be broken or continued with the use of these statements. Let’s explore these keywords in further detail.
Usage of the Python break and continue statements
The Python break and continue statements modify the behavior of the loop while the loop runs. Consider an example where you are running a loop for a specific period. At a certain point, you want the loop to end and move to the next statement within your code.
At such a point, the break statement works best. Similarly, if you want the loop to skip an iteration and move to the next iteration, the continue statement is what you’d use.
1. Python break statement
The Python break statement breaks out of a loop. Look at the example below. Let’s say you want to print a list of all the odd numbers but want the loop to stop as soon as the number goes above 10.
In such a case, you can obviously specify a range, but the other option is to break out of the loop using the break statement.
Python break statement example
number = 1 #Number is initially 1 while True : #This means the loop will continue infinite time print (number) #print the number number+=2 #calculate next odd number # Now give the breaking condition if number > 10: break; #Breaks the loop if number is greater than ten print (number) #This statement won't be executed
In the given example you will see that the statement(s) after the break, don’t execute. So here, the code will stop before printing 11.
The Python break statement can be used in the for loop too. Suppose you are printing words from a list. If any words match “exit” won’t be printed and the loop will terminate. The following Python code illustrates the idea.
words = ["rain", "sun", "moon", "exit", "weather"] for word in words: #checking for the breaking condition if word == "exit" : #if the condition is true, then break the loop break; #Otherwise, print the word print (word)
2. Python continue statement
The Python continue statement is used to skip an iteration of the loop when a condition is met.
Python Continue Example
Let’s take an example here. In the code snippet below, I’m printing out all the numbers from 1 to 10.
But when the iteration reaches number 7, I want to skip it and move to printing 8.
numbers = range(1,11) ''' the range(a,b) function creates a list of number 1 to (b-1) So, in this case it would generate numbers from 1 to 10 ''' for number in numbers: #check the skipping condition if number == 7: #this statement will be executed print("7 is skipped") continue #this statement won't be executed print ("This won't be printed") #print the values #for example: #2 is double of 1 print (number*2), print ("is double of"), print (number)
Python continue output
Let’s take the same example here with the while loop instead
numbers = [ 1, 2, 4, 3, 6, 5, 7, 10, 9 ] pos = 0 #initial position is one while pos < len(numbers): #checking skipping condition if number is divisible by two, it is even if numbers[pos] % 2 == 0 : #increment the position by one pos = pos + 1 continue #print the odd number print (numbers[pos]) #increment the position by one pos = pos + 1
Python continue statement output
That brings us to the end of the Python break and continue statement tutorial. These are very handy statements to understand and are very often used in programming. If you enjoyed reading the tutorial, go ahead and let us know in the comments below!