The Python continue
statement is a control flow tool used within loops. When encountered, it immediately ends the current iteration and proceeds to the next one, skipping any remaining code in the loop's body. This statement is particularly useful for bypassing specific conditions or values while iterating over a sequence.
Python Continue Statement Syntax
The syntax for the Python Continue Statement is straightforward and specific to loop constructs. It consists of the keyword continue
on its own line within the body of a loop, such as a for
or while
loop. This statement causes the loop to immediately jump to the next iteration, bypassing the remaining code in the current loop iteration.
For example, consider a loop that iterates over a range of numbers from 1 to 5. The continue statement is used to skip the number 3.
for number in range(1, 6):
if number == 3:
continue
print(number)
Output.
1
2
4
5
In this example, when the loop reaches the number 3, the continue
statement is executed, causing the loop to skip the print statement for that iteration and proceed to the next number.
Printing Range With Python Continue Statement
Printing a range using the Python continue statement demonstrates its functionality in loop control. When implemented in a loop, the continue statement allows skipping specific range numbers. For instance, consider a scenario where we want to print numbers from 1 to 10 but skip multiples of 3.
for i in range(1, 11):
if i % 3 == 0:
continue
print(i)
Output.
1
2
4
5
7
8
10
In this code, the continue
statement is used within a for
loop. It checks if i
is a multiple of 3. When the condition is met, continue
skips the print function, effectively omitting multiples of 3 from the output.
Continue With Nested Loop
The Python continue statement can also be effectively used within nested loops. When continue is executed in an inner loop, it skips the rest of the code in that specific inner loop iteration and moves to the next iteration. However, it does not affect the execution of the outer loop.
Example.
for i in range(1, 4): # Outer loop
for j in range(1, 4): # Inner loop
if j == 2:
continue
print(f"i = {i}, j = {j}")
Output.
i = 1, j = 1
i = 1, j = 3
i = 2, j = 1
i = 2, j = 3
i = 3, j = 1
i = 3, j = 3
In this example, whenever j
equals 2, the continue
statement is triggered, causing the inner loop to skip the printing for j = 2
and proceed to the next iteration.
Continue With While Loop
Continuing with a while loop in Python involves using the continue
statement to skip certain iterations. This is particularly effective for conditions that, when met, should not execute the remaining code in the loop. The continue
statement immediately jumps to the next iteration, effectively controlling the loop's flow based on specific criteria.
Example.
i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)
Output.
1
2
4
5
In this code, when i
equals 3, the continue
statement is triggered, skipping the print function for that iteration. This results in the numbers 1, 2, 4, and 5 being printed, with 3 conspicuously absent due to the continue statement within the while loop.
Usage Of Continue Statement
The Python continue statement is employed within looping constructs like for
and while
loops. When the continue statement is executed, it interrupts the current loop iteration and immediately moves to the next iteration, skipping any code that follows it in the loop block.