The continue
statement in Python is used to skip the current iteration of a loop and move to the next iteration. Unlike the break
statement, which terminates the loop entirely, continue
only skips the remaining code in the current iteration and proceeds with the next iteration of the loop.
Syntax
continue
How the continue
Statement Works
- In a
for
Loop: When thecontinue
statement is executed, the loop skips the remaining code in the current iteration and moves to the next iteration. - In a
while
Loop: Similarly, in awhile
loop,continue
skips the remaining code and reevaluates the loop condition to determine whether to continue with the next iteration.
Examples of Using the continue
Statement
1. Using continue
in a for
Loop
Let’s consider an example where the continue
statement is used to skip over certain iterations in a loop.
for number in range(1, 11):
if number % 2 == 0:
continue # Skip the current iteration if the number is even
print(number)
Output:
1
3
5
7
9
- Explanation: The loop iterates over the numbers from 1 to 10. When the number is even (i.e., divisible by 2), the
continue
statement is executed, which skips theprint()
function for that iteration. As a result, only odd numbers are printed.
2. Using continue
in a while
Loop
Here’s an example of using the continue
statement within a while
loop:
i = 0
while i < 10:
i += 1
if i % 2 == 0:
continue # Skip the current iteration if i is even
print(i)
Output:
1
3
5
7
9
- Explanation: The
while
loop incrementsi
from 1 to 10. Wheni
is even, thecontinue
statement is executed, skipping theprint()
function for that iteration. Therefore, only odd numbers are printed.
Practical Example of continue
Skipping Specific Values in a Loop
Suppose you want to print all numbers from 1 to 10 but skip the numbers 4 and 6.
for number in range(1, 11):
if number == 4 or number == 6:
continue # Skip the current iteration if the number is 4 or 6
print(number)
Output:
1
2
3
5
7
8
9
10
- Explanation: The loop skips printing the numbers
4
and6
and continues with the rest.
Using continue
with Nested Loops
When using nested loops, the continue
statement applies only to the loop in which it is placed.
Example with Nested Loops
for i in range(3):
for j in range(5):
if j == 3:
continue # Skip the current iteration when j equals 3
print(f"i = {i}, j = {j}")
Output:
i = 0, j = 0
i = 0, j = 1
i = 0, j = 2
i = 0, j = 4
i = 1, j = 0
i = 1, j = 1
i = 1, j = 2
i = 1, j = 4
i = 2, j = 0
i = 2, j = 1
i = 2, j = 2
i = 2, j = 4
- Explanation: The inner loop skips the iteration where
j
equals3
and continues with the next value ofj
. The outer loop continues as usual.
Key Points to Remember
- Skips Remaining Code: The
continue
statement causes the loop to skip the remaining code in the current iteration and immediately proceed to the next iteration. - Applies to the Current Loop Only: If used in nested loops,
continue
only affects the loop in which it is placed. - Useful for Skipping Specific Conditions:
continue
is useful when you want to skip certain conditions but continue with the loop.
The continue
statement is a handy tool in Python programming, allowing you to fine-tune the flow of loops and skip unnecessary computations or iterations based on specific conditions.