Continue Statement in Python
Continue is also a loop control statement just like the break statement. continue statement is opposite to that of the break statement, instead of terminating the loop, it forces to execute the next iteration of the loop. As the name suggests the continue statement forces the loop to continue or execute the next iteration. When the continue statement is executed in the loop, the code inside the loop following the continue statement will be skipped and the next iteration of the loop will begin.
Syntax of Continue Statement
The continue statement in Python has the following syntax:
for / while loop:
# statement(s)
if condition:
continue
# statement(s)
Working of Python Continue Statement
The working of the continue statement in Python is depicted in the following flowchart:
Example:
In this example, we will use Python continue statement with for loop to iterate through a range of numbers and to continue to the next iteration without performing the operation on that particular element when some condition is met.
Python3
# Python program to # demonstrate continue # statement # loop from 1 to 10 for i in range ( 1 , 11 ): # If i is equals to 6, # continue to next iteration # without printing if i = = 6 : continue else : # otherwise print the value # of i print (i, end = " " ) |
Output:
1 2 3 4 5 7 8 9 10
break, continue and pass in Python
Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by loop control statements. Loop control statements change execution from their normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements: