See how you stack up against top hiring criteria for the role in 2025.
Compare against 1000+ live job postings
Identify critical technical skill gaps
Get a personalized improvement roadmap
No signup required, takes less than 30 sec
Introduction
In Python, the continue statement skips the rest of the current loop iteration and immediately jumps to the next one. It helps you bypass certain parts of the loop based on specific conditions, giving you more control over the loop's execution flow. This is very useful in many situations, like filtering out special cases, performance optimization, complex condition logic, handling invalid inputs etc.
In this article, we will discuss the syntax of the continue statement, see how it works with flowcharts and code examples, and learn how to use it in various scenarios.
Python Continue Statement
The continue statement in Python skips the remaining code within a loop for the current iteration and moves the control back to the start of the loop for the next iteration. When the continue statement is encountered inside a loop, the program immediately jumps to the next iteration of the loop, skipping the execution of the remaining code in the current iteration.
The continue statement is useful when you want to skip specific iterations based on certain conditions without terminating the entire loop. It allows you to selectively ignore or bypass certain parts of the loop based on your program's logic.
For example:
for i in range(1, 6):
if i == 3:
continue
print(i)
You can also try this code with Online Python Compiler
In this example, the loop iterates from 1 to 5. When `i` equals 3, the continue statement is encountered, and the rest of the code block is skipped for that iteration. As a result, the number 3 is not printed, and the loop continues with the next iteration.
The continue statement is very useful when you want to filter out certain values or skip specific iterations based on conditions within the loop.
Python Continue Statement Syntax
The syntax for using the continue statement in Python is straightforward. It consists of the `continue` keyword followed by a colon (`:`) and is typically used inside a loop (such as a for loop or a while loop).
The syntax is:
for item in iterable:
if condition:
continue
# Code to be executed if the condition is not met
You can also try this code with Online Python Compiler
When the `continue` statement is encountered within the loop, the program immediately jumps to the next iteration of the loop, skipping the remaining code in the current iteration.
It's important to note that the `continue` statement is typically used in conjunction with an if statement or some other conditional logic. It is executed only if the specified condition is met.
For example:
for num in range(1, 11):
if num % 2 == 0:
continue
print(num)
You can also try this code with Online Python Compiler
In this example, the loop iterates from 1 to 10. If the current number is even (i.e., divisible by 2), the `continue` statement is executed, & the `print` statement is skipped. As a result, only the odd numbers are printed.
Flowchart of Continue Statement
To better understand the flow of execution when using the continue statement, let's take a look at a flowchart representation:
In this flowchart:
The loop begins & checks the loop condition.
If the condition is true, it executes the code within the loop body.
If a continue statement is encountered, the program immediately skips the remaining code in the loop body for the current iteration.
The loop then moves to the next iteration & repeats the process from step 1.
If the loop condition becomes false, the loop ends.
Python Continue Statement Examples
Let's look at a few examples to understand the use of the continue statement in Python.
1. Skipping even numbers
for num in range(1, 11):
if num % 2 == 0:
continue
print(num)
You can also try this code with Online Python Compiler
In this example, the loop iterates from 1 to 10. If the current number is even (divisible by 2), the continue statement is executed, skipping the print statement. As a result, only the odd numbers are printed.
2. Skipping specific items in a list
fruits = ["apple", "banana", "cherry", "date"]
for fruit in fruits:
if fruit == "banana":
continue
print(fruit)
You can also try this code with Online Python Compiler
Here, the loop iterates over the elements in the `fruits` list. If the current fruit is "banana", the continue statement is encountered, & the print statement is skipped. Consequently, "banana" is not printed, while all other fruits are printed.
3. Skipping iterations based on user input
while True:
user_input = input("Enter a number (or 'q' to quit): ")
if user_input == 'q':
break
if not user_input.isdigit():
print("Invalid input. Please enter a number.")
continue
number = int(user_input)
print("Squared:", number ** 2)
You can also try this code with Online Python Compiler
Enter a number (or 'q' to quit): abc
Invalid input. Please enter a number.
Enter a number (or 'q' to quit): 5
Squared: 25
Enter a number (or 'q' to quit): q
In this example, the loop repeatedly prompts the user for input. If the user enters 'q', the loop is terminated using the `break` statement. If the input is not a valid digit, the continue statement is executed, skipping the rest of the code block and prompting the user again. If a valid number is entered, the program proceeds to calculate and print its square.
Printing Range with Python Continue Statement
Let's see how we can use the continue statement to print a specific range of numbers while skipping certain values.
Example
for num in range(1, 16):
if 6 <= num <= 10:
continue
print(num)
You can also try this code with Online Python Compiler
In this example, the loop iterates from 1 to 15. The continue statement is used with the condition `6 <= num <= 10`. This means that when the value of `num` is between 6 and 10 (inclusive), the continue statement is executed, and the print statement is skipped. As a result, the numbers 6, 7, 8, 9, and 10 are not printed, while all other numbers in the range are printed.
Continue with Nested Loops
The continue statement can also be used within nested loops to control the flow of execution in the inner loop while continuing the iteration of the outer loop.
Let’s take an example that shows the use of continue with nested loops:
for i in range(1, 4):
for j in range(1, 4):
if i == 2 and j == 2:
continue
print(f"({i}, {j})")
You can also try this code with Online Python Compiler
In this example, we have two nested loops. The outer loop iterates over the values of `i` from 1 to 3, and the inner loop iterates over the values of `j` from 1 to 3.
Inside the inner loop, there is a condition `if i == 2 and j == 2`. When this condition is met (i.e., when both `i` and `j` are equal to 2), the continue statement is executed. This skips the print statement for the iteration where `i` is 2 and `j` is 2.
As a result, the output shows all the combinations of `(i, j)` except for `(2, 2)`, which is skipped due to the continue statement.
Continue with While Loop
The continue statement can also be used within a while loop to skip the remaining code in the current iteration and move to the next iteration.
Let’s discuss an example that shows the use of continue with a while loop:
count = 0
while count < 5:
count += 1
if count == 3:
continue
print(count)
You can also try this code with Online Python Compiler
In this example, we have a while loop that runs as long as `count` is less than 5. Inside the loop, we increment the value of `count` by 1 in each iteration.
The loop also includes an if statement that checks if `count` is equal to 3. When this condition is met, the continue statement is executed, skipping the print statement for that particular iteration.
As a result, the output shows the values of `count` as 1, 2, 4, and 5, skipping the value 3 due to the continue statement.
Usage of Continue Statement
The continue statement is useful in various scenarios where you want to skip specific iterations of a loop based on certain conditions. Let’s look at a few common use cases:
1. Filtering data
You can use the continue statement to filter out unwanted elements from a list or skip processing certain items based on specific criteria.
Example:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for num in numbers:
if num % 2 == 0:
continue
print(num)
You can also try this code with Online Python Compiler
In this example, the continue statement is used to skip even numbers and print only the odd numbers from the list.
2. Handling invalid input
When processing user input, you can use the continue statement to handle invalid or unwanted input and prompt the user again.
Example:
while True:
user_input = input("Enter a positive number (or 'q' to quit): ")
if user_input == 'q':
break
if not user_input.isdigit() or int(user_input) <= 0:
print("Invalid input. Please enter a positive number.")
continue
number = int(user_input)
print("Square:", number ** 2)
You can also try this code with Online Python Compiler
In this example, the continue statement is used to handle invalid input. If the user enters a non-numeric value or a non-positive number, the continue statement is executed, and the user is prompted again.
3. Skipping unnecessary computations
If certain conditions are met, and you don't need to perform certain computations or actions within a loop, you can use the continue statement to skip those iterations.
Example:
for i in range(1, 10):
if i % 3 == 0:
continue
result = i * 2
print(result)
You can also try this code with Online Python Compiler
In this example, the continue statement skips the iterations where `i` is divisible by 3, avoiding unnecessary computations.
Frequently Asked Questions
Can the continue statement be used with any type of loop in Python?
Yes, the continue statement can be used with both for loops and while loops in Python.
Does the continue statement terminate the loop entirely?
No, the continue statement only skips the rest of the current iteration and moves to the next iteration of the loop.
Is it necessary to use the continue statement in every loop?
No, the continue statement is optional and should be used only when you need to skip specific iterations based on certain conditions.
Conclusion
In this article, we have discussed the Python continue statement and its use within loops. We learned about its syntax and how it can be used to skip the remaining code within a loop for the current iteration. We also looked at different examples and scenarios where the continue statement can be helpful, like filtering data, handling invalid input, and skipping unnecessary computations.