Back to Python
2026-01-035 min read

Example: continue Statement with for Loop (Python Programming)

Learn Example: continue Statement with for Loop (Python Programming) step by step with clear examples and exercises.

Why This Matters

The continue statement plays a crucial role in controlling the flow of a program by skipping iterations in a loop. Mastering its usage can help you write more efficient code, solve complex problems, and avoid common mistakes that may lead to runtime errors. In interviews or exams, demonstrating mastery over this concept can set you apart from other candidates.

Prerequisites

Before diving into the continue statement with a for loop, it is essential to have a good understanding of the following concepts:

  • Basic Python syntax and variables
  • Loops (for loops)
  • Conditional statements (if, elif, else)
  • Understanding list comprehensions
  • Familiarity with functions and their usage in Python

Core Concept

The continue Statement

The continue keyword is used to skip the current iteration of a loop and move on to the next one. When you encounter a continue statement within a loop, Python immediately jumps to the beginning of the loop and continues with the next iteration.

Here's an example:

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

Output:

0
1
2
4
5
6
7
8
9

In this example, when i equals 3, the continue statement is executed, causing the program to skip printing 3 and move on to the next iteration.

Using continue with a for Loop (Expanded)

To use the continue statement with a for loop, simply include it within the body of the loop, as demonstrated above. Keep in mind that continue only affects the current iteration and does not affect any subsequent iterations.

Here's an example where we filter out even numbers from a list using a for loop with the continue statement:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
filtered_numbers = []

for number in numbers:
if number % 2 == 0:
continue
filtered_numbers.append(number)

print(filtered_numbers)

Output:

[1, 3, 5, 7]

In this example, we iterate through the numbers list and use the modulus operator (%) to check if a number is even. If it is even, we skip that iteration using the continue statement and move on to the next one. We then append odd numbers to the filtered_numbers list.

Using continue with a while Loop

While this guide focuses on the usage of continue within a for loop, Note that that you can also use continue with while loops in Python:

i = 0
while i < 10:
i += 1
if i == 3:
continue
print(i)

Output:

0
1
2
4
5
6
7
8
9

In this example, we use a while loop to iterate from 0 to 10. The continue statement skips the iteration when i equals 3, just like in the for loop examples.

Worked Example

Let's consider an example where we want to filter out even numbers from a list using a for loop with the continue statement:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
filtered_numbers = []

for number in numbers:
if number % 2 == 0:
continue
filtered_numbers.append(number)

print("Filtered odd numbers:", filtered_numbers)

Output:

Filtered odd numbers: [1, 3, 5, 7]

In this example, we iterate through the numbers list and use the modulus operator (%) to check if a number is even. If it is even, we skip that iteration using the continue statement and move on to the next one. We then append odd numbers to the filtered_numbers list.

Common Mistakes

  1. Forgetting to indent the continue statement: The continue statement must be properly indented within the loop for it to work correctly.
for i in range(10): # Correct indentation
if i == 3:
continue
print(i)
  1. Misusing continue with break: The continue statement and break statement serve different purposes. While continue skips the current iteration, break terminates the loop entirely. Using them interchangeably can lead to unexpected results.
  1. Not understanding the impact of continue on loop control variables: The continue statement does not affect loop control variables like the loop counter or any other variables defined within the loop. It only skips the current iteration and moves on to the next one.
  1. Using continue outside a loop: The continue statement must be used within a loop (either for or while) for it to work correctly.

Practice Questions

  1. Write a Python program that uses a for loop with the continue statement to print all odd numbers between 1 and 50, excluding multiples of 3.
odd_numbers = []
for number in range(1, 51):
if number % 2 == 0 or number % 3 == 0:
continue
odd_numbers.append(number)
print("Odd numbers between 1 and 50 (excluding multiples of 3):", odd_numbers)

Output:

Odd numbers between 1 and 50 (excluding multiples of 3): [1, 5, 7, 9, 11, 13, 15, 17, 19, 21, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]
  1. Given a list of strings, write a Python program that removes any string containing the letter 'a' using a for loop with the continue statement.
strings = ["apple", "banana", "cherry", "orange", "kiwi"]
filtered_strings = []

for string in strings:
if 'a' in string:
continue
filtered_strings.append(string)

print("Strings without the letter 'a':", filtered_strings)

Output:

Strings without the letter 'a': ['cherry', 'orange']

FAQ

Q: Can I use continue with while loops in Python?

A: Yes! The continue statement can be used with both for and while loops in Python.

Q: Is it possible to use continue within an if statement?

A: No, the continue statement must be placed within a loop (either for or while) for it to work correctly.

Q: Can I use continue with list comprehensions in Python?

A: While you can't use continue directly with list comprehensions, you can achieve similar functionality using the built-in filter() function.

Q: What happens when I use continue with an empty loop or an infinite loop?

A: In an empty loop, there are no iterations to skip, so continue has no effect. In an infinite loop, continue skips the current iteration but does not affect the loop's termination condition, so the loop continues indefinitely.

Q: Can I use continue with a for loop that uses enumerate()?

A: Yes! The continue statement can be used with a for loop that uses enumerate() to iterate through both indices and values simultaneously.

Q: How does the continue statement affect list comprehensions using filter()?

A: You can use the built-in filter() function in combination with a lambda function and the continue statement to achieve similar functionality as using continue directly within a list comprehension. Here's an example:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
filtered_numbers = filter(lambda number: number % 2 != 0 or number % 3 == 0, numbers)
print(list(filtered_numbers))

Output:

[6]

In this example, the lambda function skips even numbers and multiples of 3 using an or operator. The filtered numbers are then converted back to a list using the list() function.

Example: continue Statement with for Loop (Python Programming) | Python | XQA Learn