Python continue Statement Explained with Examples

The continue statement in Python is used to skip the current iteration of a loop and move on to the next iteration. When a continue statement is encountered, the loop skips the rest of the current iteration and jumps to the next iteration.

Syntax

for variable in iterable:
    # loop body
    if condition:
        continue
    # rest of the loop body
# next statement after the loop

or

while condition:
    # loop body
    if another_condition:
        continue
    # rest of the loop body
# next statement after the loop

When the continue statement is encountered within a loop, the remaining code inside the loop for the current iteration is skipped, and the loop proceeds directly to the next iteration.

Using continue in a for loop

Example:

for i in range(6):
    if i == 3:
        continue
print(i)

Output:

0
1
2
4
5

Using continue in a while loop

Example:

count = 0
while count < 5:
    count += 1
    if count == 3:
        continue
    print(count)

Output:

1
2
4
5

Skipping Even Numbers

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for number in numbers:
    if number % 2 == 0:
        continue
print(number)

Skipping Specific Characters

text = "Hello, World!"
for char in text:
    if char.lower() in ['a', 'e', 'i', 'o', 'u']:
        continue
    print(char)