Continue Statement in Python

A continue statement is used to skip one repetition or loop when the condition is met. That allows the loop to run the next loop instead. It is like saying skip this code, on to the next loop. This statement does not bring the control out of the loop like the break statement.

Let us explain with some examples:

In this first example, we will skip the loop if the current number is 6. Let us use a while loop.

number = 0
while number < 10:

    number += 1
    if number == 6:
        continue
    print(number)

Check out the output in the screenshot below and see what happens on 6:

You have used the continue statement to tell Python that once you get to 6, skip and go to 7.

Another example using the for loop. We will skip the loop when it gets to 6. The same result, different loop.

for number in range(1, 11):
if number == 6:

        continue
    print(number)

We will get the same result.

Do you want to become an expert programmer? See books helping our students, interns, and beginners in software companies today (Click here)!

Leave a Comment

Python Tutorials