Python Continue Statement
The continue
statement is used to skip an iteration of a loop if a certain condition is met. When a continue
statement is encountered inside a loop, the program execution skips the current iteration of the loop and moves to the next iteration.
Syntax
continue
Example
for num in range(1, 6):
if num == 3:
continue
print(num)
Output
1
2
4
5
Explanation
In the above example, a for
loop is used to iterate over a range of numbers from 1 to 5. The if
statement checks if the current value of num
is equal to 3. If the condition is met, the continue
statement is executed and the program skips the rest of the statements in the current iteration. The print()
statement is not executed for the value of num
equal to 3.
Use
The continue
statement is used to skip certain iterations in a loop based on certain conditions. It can be used in for
loops, while
loops, and nested loops.
Important Points
- The
continue
statement only works inside loops. - The
continue
statement can only be used withfor
loops,while
loops, and nested loops.
Summary
In this tutorial, we learned about the continue
statement in Python. We looked at its syntax, how it can be used to skip certain iterations in loops, and its important points. The continue
statement is a useful tool when working with loops, and understanding how it works can help make your code more efficient and easier to read.