Python continue
Python Continue Statement
Python continue statement is used to skip further instruction in the loop for that iteration.
In this tutorial, we shall see example programs to use continue statement with different looping statements.
Syntax
Following is the syntax of continue statement.
continue
Just the continue keyword in the line and nothing else.
Examples
1. Continue statement with While loop
In this example, we shall write a while loop to print numbers from 1 to 10. When when the loop is running to print 4 or 7, we shall conditionally execute continue statement.
Python Program
i = 1
while i <= 10 :
if i == 4 or i==7 :
i += 1
continue
print(i)
i += 1
Please note that, we have taken care of updating loop control variable i
before executing continue statement.
Output
1
2
3
5
6
8
9
10
2. Continue statement with For loop and range
In this example, we shall continue the execution of for loop when the element is divisible by 3
.
Python Program
for x in range(1, 11) :
if x%3 == 0 :
continue
print(x)
Output
1
2
4
5
7
8
10
3. Break statement with For loop and List
In this example, we shall iterate over list using for loop. When we get an element of 9
, we shall continue with next elements.
Python Program
myList = [9, 1, 5, 9, 4, 9, 7, 2, 9]
for x in myList :
if x == 9:
continue
print(x)
Output
1
5
4
7
2
Summary
In this tutorial of Python Examples, we learned how to use continue statement to skip the execution of further statements during that iteration and continue with next iterations or elements in the collection.