Python Continue Statement

The continue statement is used to skip the current iteration of a loop and move to the next iteration. Unlike the break statement, continue does not stop the loop. It only skips the current iteration. The continue statement can be used in both for and while loops. Syntax Continue in a For Loop Example: Output: […]

2 mins read Lesson 7 of 43 16% through track

The continue statement is used to skip the current iteration of a loop and move to the next iteration.

Unlike the break statement, continue does not stop the loop. It only skips the current iteration.

The continue statement can be used in both for and while loops.

Syntax

continue

Continue in a For Loop

Example:

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

    print(i)

Output:

1
2
4
5

When i becomes 3, the continue statement skips the print() statement.

Continue in a While Loop

Example:

i = 0

while i < 5:
    i += 1

    if i == 3:
        continue

    print(i)

Output:

1
2
4
5

Continue with Lists

Example:

fruits = ["Apple", "Banana", "Mango"]

for fruit in fruits:
    if fruit == "Banana":
        continue

    print(fruit)

Output:

Apple
Mango

The loop skips "Banana" and continues with the next item.

Continue with Even Numbers

Example:

for i in range(1, 11):
    if i % 2 != 0:
        continue

    print(i)

Output:

2
4
6
8
10

Only even numbers are printed.

Continue in Nested Loops

Example:

for i in range(1, 4):
    for j in range(1, 4):
        if j == 2:
            continue

        print(i, j)

Output:

1 1
1 3
2 1
2 3
3 1
3 3

The continue statement only affects the current loop.

Difference Between Break and Continue

break exits the loop completely.

Example:

for i in range(5):
    if i == 3:
        break

    print(i)

Output:

0
1
2

continue skips the current iteration and continues.

Example:

for i in range(5):
    if i == 3:
        continue

    print(i)

Output:

0
1
2
4

Practical Example

Skip empty names in a list.

Example:

names = ["John", "", "David", "", "Alice"]

for name in names:
    if name == "":
        continue

    print(name)

Output:

John
David
Alice

Common Mistake

Be careful when using continue in a while loop.

Incorrect:

i = 0

while i < 5:
    if i == 3:
        continue

    i += 1

This creates an infinite loop because i never increases when it equals 3.

Correct:

i = 0

while i < 5:
    i += 1

    if i == 3:
        continue

    print(i)

Summary

  • The continue statement skips the current iteration.
  • It does not stop the loop.
  • It can be used in for and while loops.
  • In nested loops, it affects only the current loop.
  • Use continue when you want to ignore specific values or conditions.