Python

Continue Statement In Python – Detailed Answers

In a loop, the continue statement is used to skip the remaining statements for the current iteration and jump to the beginning of the loop for the subsequent iteration. When a condition is met, the loop is terminated by the break and continue statements, and the current iteration is skipped by the latter.

Python continue statement syntax
Similar to what we have seen in Java, the syntax of the continue statement in Python (except the semicolon)

continue

A continue statement example

Let’s imagine that we wish to print only the odd numbers from a list of numbers. Using the continue statement, we can achieve this.
When the number is even, we skip the print statement inside the loop by using the continue statement; as a result, all even numbers are skipped, and the print statement is executed for all odd numbers.

# program to display only odd numbers
for num in [20, 11, 9, 66, 4, 89, 44]:
    # Skipping the iteration when number is even
    if num%2 == 0:
        continue
    # This statement will be skipped for all even numbers
    print(num)

Output:

11
9
89

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button