Back to Python
2026-01-309 min read

The continue Statement (Python Programming)

Learn The continue Statement (Python Programming) step by step with clear examples and exercises.

Title: The continue Statement (Python Programming)

Why This Matters

In Python programming, the continue statement is a crucial tool that allows developers to skip certain iterations of a loop without exiting it entirely. Understanding how to use continue can help you write more efficient and effective code, especially when dealing with complex loops or handling specific conditions. This knowledge is essential for tackling real-world programming problems and acing programming interviews.

Prerequisites

Before diving into the continue statement, it's important to have a solid understanding of the following concepts:

  1. Loops (for and while loops)
  2. Conditional statements (if, elif, else)
  3. Variables and data types in Python
  4. Basic input/output operations
  5. Understanding how loops work and how they iterate through a sequence or range of values
  6. Comprehending the structure and syntax of Python control flow statements
  7. Familiarity with common programming concepts such as variables, functions, and error handling

Core Concept

The continue statement is used within a loop to skip the current iteration and move on to the next one. When you encounter a continue statement inside a loop, the program will immediately jump to the beginning of the loop and continue with the next iteration.

Here's an example of using the continue statement in a for loop:

for i in range(10):
if i % 2 == 0:
continue
print(i)

In this example, we have a for loop that iterates over numbers from 0 to 9. We use an if statement to check if the current number is even (i % 2 == 0). If it is, we use the continue statement to skip that iteration and move on to the next one. The output of this code will be:

1
3
5
7
9

You can also use continue in a while loop, like so:

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

This will produce the same output as the previous example.

Understanding the Effect of continue

It's important to understand that when you use continue, the current iteration is skipped, but the loop continues to run. This means that any code after the continue statement within the current iteration will not be executed. Instead, the program moves on to the next iteration immediately.

Nesting Loops and continue

You can use continue in nested loops as well. When you encounter a continue statement within a nested loop, it will skip the current iteration of the innermost loop and continue with the next one. Here's an example:

for i in range(10):
for j in range(10):
if i == 5 and j == 5:
continue
print(i, j)

In this example, we have a nested loop that iterates over numbers from 0 to 9 for both i and j. We use an if statement to check if the current values of i and j are both 5. If they are, we use the continue statement to skip that iteration and move on to the next one in the innermost loop. The output of this code will be:

0 0
0 1
0 2
0 3
0 4
1 0
1 1
1 2
1 3
1 4
2 0
2 1
2 2
2 3
2 4
3 0
3 1
3 2
3 3
3 4
4 0
4 1
4 2
4 3
4 4
5 0
5 1
5 2
5 3
5 4
6 0
6 1
6 2
6 3
6 4
7 0
7 1
7 2
7 3
7 4
8 0
8 1
8 2
8 3
8 4
9 0
9 1
9 2
9 3
9 4

Notice that the iteration where i is 5 and j is 5 has been skipped.

Worked Example

Let's say we have a list of numbers and we want to find all the prime numbers in the list. We can use a for loop, the continue statement, and some basic number theory to achieve this:

def find_primes(numbers):
primes = []
for num in numbers:
if num <= 1:
continue
for i in range(2, num):
if num % i == 0:
continue
primes.append(num)
return primes

numbers = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
print(find_primes(numbers))

In this example, we define a function find_primes that takes a list of numbers as an argument. Inside the function, we initialize an empty list to store the prime numbers. We then iterate over each number in the input list using a for loop.

For each number, we first check if it's less than or equal to 1 (in which case it's not a prime number). If it is, we use continue to skip that iteration and move on to the next one.

If the number is greater than 1, we then use another for loop to test whether the number can be divided evenly by any number from 2 up to (but not including) itself. If it can, we again use continue to skip that iteration and move on to the next one.

If the number passes all these tests, we append it to our list of prime numbers. Finally, we return this list at the end of the function.

Running this code with the provided list will output:

[5, 7, 11]

Understanding the Worked Example

In this example, we first check if a number is less than or equal to 1 using an if statement. If it is, we use continue to skip that iteration and move on to the next one because numbers less than 2 are not prime numbers.

Next, we use another for loop to test whether the current number can be divided evenly by any number from 2 up to (but not including) itself. We do this by checking if the remainder of the division operation between the current number and the divisor is zero (num % i == 0). If it is, we use continue to skip that iteration and move on to the next one because a number that can be divided evenly by another number is not prime.

If the number passes both tests, we append it to our list of prime numbers.

Common Mistakes

  1. ### Using continue outside a loop

The continue statement only works within loops (for and while). If you try to use it outside a loop, you'll get a syntax error.

  1. ### Misunderstanding the effect of continue

Some programmers might mistake continue for an exit command, thinking that it will end the loop entirely when encountered. In reality, continue only skips the current iteration and continues with the next one.

  1. ### Skipping important checks before using continue

When using continue, make sure you're not skipping any important iterations or conditions that should be handled differently. For example, in our prime number example, we need to check if a number is greater than 1 before testing its divisibility.

  1. ### Using continue without proper indentation

Proper indentation is essential when using the continue statement. If you don't properly indent your code, the continue statement may not work as intended or may cause syntax errors.

  1. ### Overusing continue

While continue can be a powerful tool, it's important to use it judiciously. Overusing continue can make your code harder to read and understand, so try to find the right balance between using continue and other control flow statements like if, elif, and else.

Practice Questions

  1. Write a Python program that prints all the even numbers between 1 and 50 using a for loop and the continue statement.
  2. Modify the prime number example to find all the prime numbers in the range 1 to 100.
  3. Write a Python program that removes all the duplicate elements from a given list using a for loop, the continue statement, and a temporary storage variable.
  4. Write a Python program that finds all the Fibonacci numbers up to 100 using a while loop and the continue statement.
  5. Write a Python program that calculates the sum of all even numbers between 1 and 100 using a for loop and the continue statement.
  6. Write a Python program that finds all the prime numbers in a user-defined range using a while loop, input function, and the continue statement.
  7. Write a Python program that finds the smallest prime number greater than a given number using a while loop, input function, and the continue statement.
  8. Write a Python program that finds all the perfect squares in a given range using a for loop, square root function, and the continue statement.
  9. Write a Python program that finds all the prime factors of a given number using a while loop, input function, and the continue statement.
  10. Write a Python program that checks if a given number is a prime number using a while loop, input function, and the continue statement.

FAQ

### What happens when I use continue inside an empty loop?

If you use continue inside an empty loop (i.e., a loop with no iterations), nothing will happen because there are no iterations to skip. The program will simply continue executing after the loop.

### Can I use continue in nested loops?

Yes, you can use continue in nested loops as well. When you encounter a continue statement within a nested loop, it will skip the current iteration of the innermost loop and continue with the next one.

### Is there a way to "unskip" an iteration that was skipped using continue?

No, once an iteration has been skipped using continue, it cannot be undone. If you need to handle the skipped iteration differently, consider using an if statement or another loop control structure instead of continue.

### Can I use continue with a break statement?

Yes, you can use both continue and break statements in the same loop. When you encounter a break statement within a loop, the loop will exit immediately. If you use continue before the break statement, the current iteration will be skipped, but the loop will continue running until the break statement is encountered.

### Can I use continue with a pass statement?

No, you cannot use continue with a pass statement directly. The pass statement is used as a placeholder when syntax requires a statement but you don't want any code to be executed. If you want to use continue within a loop that contains a pass statement, simply remove the pass statement or replace it with actual code.

### Can I use continue with a return statement?

No, you cannot use continue with a return statement directly. The return statement is used to exit a function and return a value to the caller. If you want to use continue within a loop that is nested inside a function, consider using a global variable or passing a callback function to the outer function instead of returning from the inner function.

### Can I use continue with a yield statement?

No, you cannot use continue with a yield statement directly. The yield statement is used in generator functions to suspend and resume execution at specific points. If you want to use continue within a generator function, consider using a loop control variable or a sentinel value instead of using continue.

### Can I use continue with a raise statement?

No, you cannot use continue with a raise statement directly. The raise statement is used to throw an exception and propagate it up the call stack. If you want to handle exceptions within a loop, consider using a try-except block instead of using continue.

The continue Statement (Python Programming) | Python | XQA Learn