Skipping Iterations via continue

๐Ÿท๏ธ Loops and Iteration / Loop Control Statements

๐Ÿง  Context Introduction

When working with loops, there are times when you want to skip the current iteration and move directly to the next oneโ€”without breaking out of the loop entirely. This is where the continue statement comes in. It tells Python: "Stop here for this round, ignore everything after me, and jump to the next iteration."

This is especially useful when you need to filter out certain values, skip errors, or avoid processing unwanted data without stopping the loop.


โš™๏ธ How continue Works

  • The continue statement is used inside for or while loops.
  • When Python encounters continue, it immediately skips the rest of the code in the current iteration.
  • The loop then moves to the next iteration (next item in a for loop, or next condition check in a while loop).
  • continue does not exit the loopโ€”it only skips one round.

๐Ÿ•ต๏ธ Simple Example: Skipping Even Numbers

Imagine you want to print only odd numbers from 1 to 5.

  • You loop through numbers 1, 2, 3, 4, 5.
  • If the number is even, you use continue to skip printing it.
  • Only odd numbers get printed.

Code example:

Loop through numbers 1 to 5. Inside the loop, check if the number is even using number % 2 == 0. If true, execute continue to skip the rest of the iteration. Otherwise, print the number.

Expected output:

1
3
5


๐Ÿ› ๏ธ Practical Example: Skipping Invalid Data

When processing a list of data, you might encounter empty or invalid entries. Use continue to skip them.

Scenario: You have a list of server names, but some entries are empty strings. You want to print only the valid server names.

Code example:

Create a list of server names: ["web-01", "", "db-01", "", "cache-01"]. Loop through each server. Inside the loop, check if the server name is an empty string using if server == "". If it is empty, use continue to skip it. Otherwise, print the server name.

Expected output:

web-01
db-01
cache-01


๐Ÿ“Š Comparison: continue vs break

Feature continue break
Action Skips the current iteration Exits the loop entirely
Loop continues? Yes, moves to next iteration No, loop stops
Use case Skip unwanted values Stop when condition is met
Example Skip even numbers Stop at first error

๐Ÿงช Using continue in a while Loop

The continue statement works the same way in while loops. Be careful thoughโ€”if you forget to update the loop variable before using continue, you can create an infinite loop.

Code example:

Set a variable count = 0. Start a while loop that runs while count < 5. Inside the loop, increment count by 1 first. Then check if count == 3. If true, use continue to skip printing. Otherwise, print the current count.

Expected output:

1
2
4
5

Notice that 3 is skipped, but the loop continues to the end.


โš ๏ธ Common Pitfall: Infinite Loop with continue

If you place continue before updating your loop variable in a while loop, the variable never changes and the loop runs forever.

Bad example:

Set x = 0. Start a while loop with condition x < 5. Inside, check if x == 2 and use continue. Then increment x by 1. The continue skips the increment when x is 2, so x stays 2 forever.

Fix: Always update the loop variable before the continue statement, or ensure the update happens before any conditional skip.


โœ… Summary

  • Use continue to skip the current iteration and move to the next one.
  • It is useful for filtering data, skipping errors, or ignoring unwanted values.
  • continue does not stop the loopโ€”only the current round.
  • Be careful with while loops to avoid infinite loops by updating variables before continue.
  • Combine continue with if statements for clean, readable skipping logic.

The continue statement skips the current iteration of a loop and moves directly to the next iteration.


๐ŸŸข Example 1: Skipping a single number

This example shows how continue skips the number 3 and continues with the next iteration.

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

๐Ÿ“ค Output: 1 2 4 5


๐ŸŸข Example 2: Skipping even numbers

This example demonstrates using continue to skip all even numbers and only print odd numbers.

for number in range(1, 11):
    if number % 2 == 0:
        continue
    print(number)

๐Ÿ“ค Output: 1 3 5 7 9


๐ŸŸข Example 3: Skipping items in a list

This example shows how to skip a specific string value while iterating through a list.

items = ["engineer", "manager", "engineer", "intern", "engineer"]
for person in items:
    if person == "manager":
        continue
    print(person)

๐Ÿ“ค Output: engineer engineer intern engineer


๐ŸŸข Example 4: Skipping negative values in calculations

This example demonstrates skipping negative numbers during a summation to only add positive values.

numbers = [5, -3, 10, -8, 15]
total = 0
for value in numbers:
    if value < 0:
        continue
    total = total + value
print(total)

๐Ÿ“ค Output: 30


๐ŸŸข Example 5: Skipping invalid user input in a while loop

This example shows how continue can skip empty or invalid input and prompt the user again.

names = []
while len(names) < 3:
    name = input("Enter a name: ")
    if name == "":
        print("Name cannot be empty. Try again.")
        continue
    names.append(name)
print("Collected names:", names)

๐Ÿ“ค Output: (depends on user input, but will only accept 3 non-empty names)


๐Ÿ“Š Comparison: break vs continue

Statement Action Effect on Loop
break Exits the loop entirely Loop stops immediately
continue Skips the current iteration Loop continues with next iteration

๐Ÿง  Context Introduction

When working with loops, there are times when you want to skip the current iteration and move directly to the next oneโ€”without breaking out of the loop entirely. This is where the continue statement comes in. It tells Python: "Stop here for this round, ignore everything after me, and jump to the next iteration."

This is especially useful when you need to filter out certain values, skip errors, or avoid processing unwanted data without stopping the loop.


โš™๏ธ How continue Works

  • The continue statement is used inside for or while loops.
  • When Python encounters continue, it immediately skips the rest of the code in the current iteration.
  • The loop then moves to the next iteration (next item in a for loop, or next condition check in a while loop).
  • continue does not exit the loopโ€”it only skips one round.

๐Ÿ•ต๏ธ Simple Example: Skipping Even Numbers

Imagine you want to print only odd numbers from 1 to 5.

  • You loop through numbers 1, 2, 3, 4, 5.
  • If the number is even, you use continue to skip printing it.
  • Only odd numbers get printed.

Code example:

Loop through numbers 1 to 5. Inside the loop, check if the number is even using number % 2 == 0. If true, execute continue to skip the rest of the iteration. Otherwise, print the number.

Expected output:

1
3
5


๐Ÿ› ๏ธ Practical Example: Skipping Invalid Data

When processing a list of data, you might encounter empty or invalid entries. Use continue to skip them.

Scenario: You have a list of server names, but some entries are empty strings. You want to print only the valid server names.

Code example:

Create a list of server names: ["web-01", "", "db-01", "", "cache-01"]. Loop through each server. Inside the loop, check if the server name is an empty string using if server == "". If it is empty, use continue to skip it. Otherwise, print the server name.

Expected output:

web-01
db-01
cache-01


๐Ÿ“Š Comparison: continue vs break

Feature continue break
Action Skips the current iteration Exits the loop entirely
Loop continues? Yes, moves to next iteration No, loop stops
Use case Skip unwanted values Stop when condition is met
Example Skip even numbers Stop at first error

๐Ÿงช Using continue in a while Loop

The continue statement works the same way in while loops. Be careful thoughโ€”if you forget to update the loop variable before using continue, you can create an infinite loop.

Code example:

Set a variable count = 0. Start a while loop that runs while count < 5. Inside the loop, increment count by 1 first. Then check if count == 3. If true, use continue to skip printing. Otherwise, print the current count.

Expected output:

1
2
4
5

Notice that 3 is skipped, but the loop continues to the end.


โš ๏ธ Common Pitfall: Infinite Loop with continue

If you place continue before updating your loop variable in a while loop, the variable never changes and the loop runs forever.

Bad example:

Set x = 0. Start a while loop with condition x < 5. Inside, check if x == 2 and use continue. Then increment x by 1. The continue skips the increment when x is 2, so x stays 2 forever.

Fix: Always update the loop variable before the continue statement, or ensure the update happens before any conditional skip.


โœ… Summary

  • Use continue to skip the current iteration and move to the next one.
  • It is useful for filtering data, skipping errors, or ignoring unwanted values.
  • continue does not stop the loopโ€”only the current round.
  • Be careful with while loops to avoid infinite loops by updating variables before continue.
  • Combine continue with if statements for clean, readable skipping logic.

Interactive Views

You are currently in ๐Ÿ“š All-in-One mode. Use the tabs at the top to switch to ๐Ÿ“– Theory Only or ๐Ÿ’ป Code Only views.

The continue statement skips the current iteration of a loop and moves directly to the next iteration.


๐ŸŸข Example 1: Skipping a single number

This example shows how continue skips the number 3 and continues with the next iteration.

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

๐Ÿ“ค Output: 1 2 4 5


๐ŸŸข Example 2: Skipping even numbers

This example demonstrates using continue to skip all even numbers and only print odd numbers.

for number in range(1, 11):
    if number % 2 == 0:
        continue
    print(number)

๐Ÿ“ค Output: 1 3 5 7 9


๐ŸŸข Example 3: Skipping items in a list

This example shows how to skip a specific string value while iterating through a list.

items = ["engineer", "manager", "engineer", "intern", "engineer"]
for person in items:
    if person == "manager":
        continue
    print(person)

๐Ÿ“ค Output: engineer engineer intern engineer


๐ŸŸข Example 4: Skipping negative values in calculations

This example demonstrates skipping negative numbers during a summation to only add positive values.

numbers = [5, -3, 10, -8, 15]
total = 0
for value in numbers:
    if value < 0:
        continue
    total = total + value
print(total)

๐Ÿ“ค Output: 30


๐ŸŸข Example 5: Skipping invalid user input in a while loop

This example shows how continue can skip empty or invalid input and prompt the user again.

names = []
while len(names) < 3:
    name = input("Enter a name: ")
    if name == "":
        print("Name cannot be empty. Try again.")
        continue
    names.append(name)
print("Collected names:", names)

๐Ÿ“ค Output: (depends on user input, but will only accept 3 non-empty names)


๐Ÿ“Š Comparison: break vs continue

Statement Action Effect on Loop
break Exits the loop entirely Loop stops immediately
continue Skips the current iteration Loop continues with next iteration