Course contentsShow
← Python
Lesson 100 of 1,0602. Control Flow and LogicFree lesson

Skipping Iterations With Continue

Learners will be able to use the continue statement to bypass the remainder of a loop body and immediately jump to the next iteration.

Skipping Iterations With Continue

In this lesson, you will learn how to use the continue statement to instantly stop the current loop iteration and jump straight to the next one.

The "Skip" Button for Loops

So far, you’ve used for and while loops to process every item in a sequence. But what if you find an item that you want to ignore? Instead of writing a complex if/else block that wraps your entire loop code, you can use continue.

When Python hits the continue keyword, it stops executing the remaining lines inside that specific loop body and immediately moves back to the top to start the next iteration. It doesn't exit the loop entirely (like break does); it just decides that the current task isn't worth finishing.

Seeing it in Action

Imagine you have a list of numbers, and you want to print the squares of only the odd numbers. You can use continue to "skip" the even numbers:

numbers = [1, 2, 3, 4, 5]

for num in numbers:
    if num % 2 == 0:
        continue  # Skip the rest of the loop for even numbers
    
    square = num * num
    print(f"The square of {num} is {square}")

In the example above, when num is 2, the continue statement triggers. Python stops, ignores the square calculation and the print function, and jumps right to the next number (3).

Why Use Continue?

Using continue is a fantastic way to keep your code clean. It prevents "arrow code," where your logic is pushed further and further to the right by deep if/else nesting. By handling the "skip" condition at the very top, you keep the rest of your logic clear and focused on the data you actually want to process.

Key Takeaway: The continue statement forces the loop to abandon the current iteration and immediately move on to the next one, allowing you to easily filter out unwanted data.