How to skip Iterations in For Loop Python (original) (raw)

Last Updated : 18 Nov, 2024

Loops are generally used to repeat the tasks. Sometimes, we may want to skip certain steps in the loop. We can do this easily with the continue statement. This article explains how to skip iterations in a for loop.

In a for loop, you can use continue to skip a specific iteration when a condition is true. When Python sees continue, it skips the rest of the current iteration and moves to the next iteration.

Python `

a = ["Geeks", "for", "Geeks_2", "Geeks_3"]

for i in a: if i == "for": # Skip "for" continue print(i)

`

Output

Geeks Geeks_2 Geeks_3

**Example #2 - Skipping Odd Numbers

Python `

for i in range(1, 11):

# Check if the number is odd
if i % 2 != 0:  
    continue
print(i)

`

Using If-else [Alternative to continue]

We can Use _if-else when both _if condition and _else block need to execute distinct logic.

Python `

using if..else to skip iteration

for num in range(1, 6): if num % 2 == 0: # Skip even numbers pass else: print(num)

`

Can we use _break to skip iterations in a loop?

The break statement is used to stop the loop completely. It does not skip iterations. If you want to skip an iteration, you should use continue statement.

Similar Reads