Python Continue Statement
The Python continue statement is used to skip the current iteration of a loop and move on to the next iteration. It’s helpful when you want to skip over certain elements in a loop but still want the loop to continue running.
Here are some common scenarios where you might use the continue statement:
- Skipping specific items: Skip certain items in a loop based on a condition.
- Filter out unwanted values: Continue to the next iteration without processing unwanted data.
What You'll Learn
In this tutorial, you'll learn the basics of the continue statement in Python, how to use it in loops, and how it helps manage loop control more effectively.
Understanding the continue Statement
The continue statement is used to skip the current iteration of a loop and move to the next one. Unlike break, which exits the loop entirely, continue only skips the current iteration, allowing the loop to continue running.
# Example of continue statement with a list
numbers = [1, 2, 3, 4, 5, 6, 7]
for number in numbers:
if number % 2 == 0:
continue # Skip even numbers
print(number) # This will print odd numbers only
# Example of continue statement with a list
numbers = [1, 2, 3, 4, 5, 6, 7]
for number in numbers:
if number % 2 == 0:
continue # Skip even numbers
print(number) # This will print odd numbers only
In the example above, the loop will print only the odd numbers. When an even number is encountered, the continue statement is executed, skipping the print statement for that iteration.
Output:
1
3
5
7
1
3
5
7
Example 1: Skipping even numbers with a for loop
You can use continue in a for loop to skip certain iterations based on a condition. Here's an example:
# Skipping even numbers
numbers = [1, 2, 3, 4, 5, 6]
for number in numbers:
if number % 2 == 0:
continue # Skip even numbers
print(number) # Print odd numbers only
# Skipping even numbers
numbers = [1, 2, 3, 4, 5, 6]
for number in numbers:
if number % 2 == 0:
continue # Skip even numbers
print(number) # Print odd numbers only
How It Works:
- for number in numbers:: This starts a loop that will go through each item in the numbers list.
- if number % 2 == 0:: The loop checks if the number is even.
- continue: Skips the current iteration when the condition is true (even number).
- So, the loop prints only the odd numbers.
Output
1
3
5
1
3
5
Example 2: Skipping negative numbers with a while loop
You can also use continue in a while loop to skip negative numbers and only print positive ones. Here's an example:
# Skipping negative numbers
i = -5
while i <= 5:
if i < 0:
i += 1
continue # Skip negative numbers
print(i)
i += 1
# Skipping negative numbers
i = -5
while i <= 5:
if i < 0:
i += 1
continue # Skip negative numbers
print(i)
i += 1
How It Works:
- while i <= 5:: This starts a loop that will run while i is less than or equal to 5.
- if i < 0:: The loop checks if i is negative.
- continue: Skips the current iteration when i is negative.
- The loop prints numbers from 0 to 5, skipping negative numbers.
Output
0
1
2
3
4
5
0
1
2
3
4
5
Example 3: Skipping vowels in a string with a for loop
You can use continue to skip over vowels while iterating through a string. Here's an example:
# Skipping vowels in a string
word = "programming"
for letter in word:
if letter in 'aeiou':
continue # Skip vowels
print(letter, end='')
# Skipping vowels in a string
word = "programming"
for letter in word:
if letter in 'aeiou':
continue # Skip vowels
print(letter, end='')
How It Works:
- for letter in word:: This starts a loop that will iterate through each character in the word "programming".
- if letter in 'aeiou':: The loop checks if the current letter is a vowel.
- continue: Skips the current iteration when the letter is a vowel.
- The loop prints the word "prgrmmng", skipping the vowels.
Output
prgrmmng
prgrmmng
Exercises
Try out the following exercises to practice using the continue statement:
1. Write a program that prints all numbers from 1 to 10 except 5 and 7.
# Exercise 1: Skip 5 and 7
for i in range(1, 11):
if i == 5 or i == 7:
continue
print(i)
# Exercise 1: Skip 5 and 7
for i in range(1, 11):
if i == 5 or i == 7:
continue
print(i)
2. Write a program that skips numbers divisible by three.
# Exercise: Skip numbers divisible by 3
while True:
user_input = input("Enter a number (or 'exit' to quit): ")
if user_input == 'exit':
break
num = int(user_input)
if num % 3 == 0:
continue
print(f"You entered: {num}")
# Exercise: Skip numbers divisible by 3
while True:
user_input = input("Enter a number (or 'exit' to quit): ")
if user_input == 'exit':
break
num = int(user_input)
if num % 3 == 0:
continue
print(f"You entered: {num}")
3. Write a program that skips the letter "a" while printing each character in a string.
# Exercise: Skip the letter 'a'
text = "banana"
for char in text:
if char == 'a':
continue
print(char)
# Exercise: Skip the letter 'a'
text = "banana"
for char in text:
if char == 'a':
continue
print(char)
*Tip: Experiment with different conditions and see how continue helps you control the flow of your loops!
Frequently Asked Questions
What does the continue statement do in Python?
What does the continue statement do in Python?
The continue statement skips the current iteration of a loop and immediately proceeds to the next iteration.
When should I use the continue statement?
When should I use the continue statement?
Use continue when you want to skip over a particular condition in a loop without stopping the loop entirely.
Can I use continue in both for and while loops?
Can I use continue in both for and while loops?
Yes, continue works with both for and while loops in Python.
What is the difference between continue and break?
What is the difference between continue and break?
continue skips to the next iteration, while break exits the loop completely.
Does continue affect the loop counter?
Does continue affect the loop counter?
In a while loop, be careful to update the loop counter before continue, or you may accidentally create an infinite loop.
What's Next?
Next, you'll learn about using the else clause with loops in Python. This unique feature lets you execute a block of code once a loop finishes normally—without being interrupted by a break statement. You'll see how it works with both for
and while
loops to write more precise and controlled logic.