Use the continue statement to skip the current iteration of a for or while loop and move on to the next iteration in Python.
Note: If the continue statement is present in a nested loop, it skips the execution of the inner loop only.
Python skip iteration
Simple example code skip the execution of the current (skip processing if the value is 3) iteration of the loop.
tn = (1, 2, 3, 4, 5)
for i in tn:
if i == 3:
continue
print(f'Processing number {i}')
print("Done")
Output:
Continue the statement with the while loop
count = 10
while count > 0:
if count % 3 == 0:
count -= 1
continue
print(f'Processing Number {count}')
count -= 1
Program to display only odd numbers
for num in [20, 11, 9, 66, 4, 89, 44]:
# Skipping the iteration when number is even
if num%2 == 0:
continue
# This statement will be skipped for all even numbers
print(num)
Do comment if you have any doubts or suggestions on this Python loop topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.