Python continue Statement
In Python, the continue statement is used inside loops (such as for or while loops) to skip the current iteration and move to the next iteration. When the continue statement is encountered, it immediately jumps to the next iteration without executing any further statements in the loop's body.
Here's the general syntax of the continue statement:
while condition:
# code
if condition:
continue
# more code
or
for item in iterable:
# code
if condition:
continue
# more code
In the above examples, if the condition specified after the if statement evaluates to True, the continue statement is executed, and the remaining code in the loop's body is skipped. The loop then proceeds to the next iteration.
Here's a simple example that demonstrates the usage of the continue statement:
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num % 2 == 0:
continue
print(num)
# Output: 1
# 3
# 5
In the above code, the continue statement is used to skip printing even numbers. When an even number is encountered, the continue statement is executed, and the print statement is bypassed. As a result, only the odd numbers are printed.
Remember that the continue statement can only be used inside loops and is used to control the flow of the loop.