Python Continue Statement (With for & while loop Examples)
Table of Contents
- Introduction
- What is Python Continue Statement?
- Python Continue Statement Syntax
- Continue in Python for Loop
- Continue in Python while Loop
- Printing range with Python Continue Statement
- Python Continue with Nested loops
- Uses of Continue Statement in Python
- Difference Between Break and Continue in Python
- Difference Between Python Continue and Pass Statements
Introduction
The continue statement in Python is used to skip the execution of a block of code and move to the next iteration.
This blog will dive deeper into the Python continue statement, flowchart, examples, and the difference between continue and pass statements. Let’s have a look.
What is Python Continue Statement?
The continue statement in Python is used to skip the rest of the code inside a loop (for or while) for the current iteration and move on to the next iteration. When the continue statement is encountered, the remaining code inside the loop for the current iteration is skipped, and the loop proceeds to the next iteration.
Actually, loops repeat a block of code or processes efficiently, but in some cases, we need to leave the current loop, skip the current iteration, or dismiss the condition of the loop. That is where the Python continue statements are used.
Python Continue Statement Syntax
The syntax for the continue in Python is as follows:
while condition:
# code block
if some_condition:
continue
# rest of the code inside the loop
or
for variable in sequence:
# code block
if some_condition:
continue
# rest of the code inside the loop
Here, condition is the condition for a while loop, and variable takes values from the sequence in a for loop. The some_condition is a placeholder for the condition that, when met, triggers the continue statement.
When the continue statement is encountered, the rest of the code inside the loop for the current iteration is skipped, and the loop moves on to the next iteration.
Continue in Python for Loop
In a for loop in Python, the continue statement is used to skip the rest of the code inside the loop for the current iteration and move on to the next iteration.
Syntax
Here's the syntax for using Python continue in a for loop:
for variable in sequence:
# code block
if some_condition:
continue
# rest of the code inside the loop
Example 1
Here's a simple example:
for i in range(5):
if i == 2:
continue
print(i)
In this example, when i is equal to 2, the continue statement is triggered, and the print(i) statement is skipped for that iteration. The loop then continues with the next value of i.
Output:
0
1
3
4
The continue statement is particularly useful when you want to skip certain iterations based on a specific condition without executing the remaining code in the loop for those iterations.
Example 2: Skip even numbers
for i in range(1, 11):
if i % 2 == 0:
continue
print(i)
Output:
1
3
5
7
9
In this example, the continue statement is used to skip even numbers, and only odd numbers are printed.
Example 3: Skip iteration based on a condition
grades = [85, 92, 78, 95, 60, 88]
for grade in grades:
if grade < 70:
print(f"Skipping grade {grade} as it's below 70.")
continue
print(f"Processing grade {grade}.")
Output:
Processing grade 85.
Processing grade 92.
Processing grade 78.
Processing grade 95.
Skipping grade 60 as it's below 70.
Processing grade 88.
In this example, the continue statement is used to skip processing grades below 70.
Example 4: Continue until a specific condition is met
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for num in numbers:
if num > 5:
print(f"Stopping at {num}.")
break
if num % 2 == 0:
print(f"Skipping even number: {num}.")
continue
print(f"Processing odd number: {num}.")
Output:
Processing odd number: 1.
Skipping even number: 2.
Processing odd number: 3.
Skipping even number: 4.
Processing odd number: 5.
Stopping at 6.
In this example, the loop processes odd numbers and skips even numbers until it encounters a number greater than 5, at which point it stops.
Continue in Python while Loop
In a while loop in Python, the continue statement is also used to skip the rest of the code inside the loop for the current iteration and move on to the next iteration.
Syntax
while condition:
# code block
if some_condition:
continue
# rest of the code inside the loop
Example 1
i = 0
while i < 5:
i += 1
if i == 2:
continue
print(i)
In this example, when i is equal to 2, the continue statement is triggered, and the print(i) statement is skipped for that iteration. The loop then continues with the next value of i.
Output:
1
3
4
5
Example 2: Skip even numbers
i = 1
while i <= 10:
if i % 2 == 0:
i += 1
continue
print(i)
i += 1
Output:
1
3
5
7
9
In this example, the continue statement is used to skip even numbers, and only odd numbers are printed in the while loop.
Example 3: Skip iteration based on a condition
grades = [85, 92, 78, 95, 60, 88]
index = 0
while index < len(grades):
if grades[index] < 70:
print(f"Skipping grade {grades[index]} as it's below 70.")
index += 1
continue
print(f"Processing grade {grades[index]}.")
index += 1
Output:
Processing grade 85.
Processing grade 92.
Processing grade 78.
Processing grade 95.
Skipping grade 60 as it's below 70.
Processing grade 88.
In this example, the continue statement is used to skip processing grades below 70 in a while loop.
Example 4: Continue until a specific condition is met
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
index = 0
while index < len(numbers):
if numbers[index] > 5:
print(f"Stopping at {numbers[index]}.")
break
if numbers[index] % 2 == 0:
print(f"Skipping even number: {numbers[index]}.")
index += 1
continue
print(f"Processing odd number: {numbers[index]}.")
index += 1
Output:
Processing odd number: 1.
Skipping even number: 2.
Processing odd number: 3.
Skipping even number: 4.
Processing odd number: 5.
Stopping at 6.
In this example, the while loop processes odd numbers, skips even numbers, and stops when it encounters a number greater than 5.
Printing range with Python Continue Statement
Suppose you need to write a program to print numbers from 1 to 10 but not 5. You must do this using a loop, and that too just one loop. That is where the continue statement will be useful.
So, you can run a loop from 1 to 10, and each time, you need to compare the value of the loop variable with 5. If it’s equal to 5, you will use the continue statement to move to the next iteration without printing anything; else, print the value.
If you want to use the continue statement in conjunction with the range function in Python, you can skip certain values within the specified range.
Example 1: Skip numbers divisible by 3
for i in range(1, 11):
if i % 3 == 0:
continue
print(i)
Output:
1
2
4
5
7
8
10
In this example, the continue statement is used to skip numbers in the range from 1 to 10 that are divisible by 3.
Example 2: Skip even numbers
for i in range(1, 11):
if i % 2 == 0:
continue
print(i)
Output:
1
3
5
7
9
In this example, the continue statement is used to skip even numbers in the range from 1 to 10.
Python Continue with Nested loops
The continue statement in Python can also be used in nested loops to skip the rest of the innermost loop's code for the current iteration and move on to the next iteration.
Example:
for i in range(1, 4):
print(f"Outer loop iteration {i}")
for j in range(1, 4):
if j == 2:
print(" Skipping inner loop iteration 2")
continue
print(f" Inner loop iteration {j}")
Output:
Outer loop iteration 1
Inner loop iteration 1
Skipping inner loop iteration 2
Inner loop iteration 3
Outer loop iteration 2
Inner loop iteration 1
Skipping inner loop iteration 2
Inner loop iteration 3
Outer loop iteration 3
Inner loop iteration 1
Skipping inner loop iteration 2
Inner loop iteration 3
In this example, the continue statement is used within the inner loop to skip iteration 2 of the inner loop, but it doesn't affect the outer loop. The continue statement only applies to the loop in which it is used.
You can use continue within any level of nested loops to skip the rest of the code for the current iteration at that level and move on to the next iteration of that specific loop
Uses of Continue Statement in Python
Following are some common use cases for the Python continue statement:
1. Skipping Specific Iterations
You can use continue in Python to skip certain iterations of a loop based on a condition. For example, you might want to skip processing certain elements in a sequence or range.
2. Handling Special Cases
The continue statement is useful for handling special cases within a loop. For instance, you might want to process most elements in a sequence but skip or handle differently certain elements.
3. Avoiding Nested If Statements
In some cases, continue in Python can be used to avoid deeply nested if statements by allowing you to skip certain conditions and move to the next iteration.
4. Early Loop Termination
You can use continue in conjunction with other conditions to terminate a loop early in certain cases.
Difference Between Break and Continue in Python
Difference Between Python Continue and Pass Statements
Most people are confused regarding the continue and pass keywords. To clarify the uses and purpose of the continue and pass keywords, here are the differences between the two.
More Tutorials:-
Compilers to Practice Online:-