Python continue Statement Complete Tutorial

In this section, we will learn what the continue statement is and how to use it in Python.

Continue Statement in Python

The continue statement is used within the body of loops like `while` and `for` loop in order to stop the current iteration of the loop and move on to the next iteration.

Note that using the continue statement won’t stop the entire loop, but instead it will just break the current iteration and jump into the next iteration of the same loop (if any iteration remained, of course).

Example: while loop continue statement

num = 0; 
while num<10:
    num +=1
    if num<5:
        continue
    print(num)

Output:

5

6

7

8

9

10

In this example, as long as the value of the `num` variable is less than the value 5, a call to the `continue` statement will occur and that will cause the current iteration in the loop to stop and the next one to start.

That’s why we didn’t get the values from 0 to 4 on the output stream.

Example: for loop continue statement

list1 = [1,2,3,4,5,6]
for element in list1:
    if element <4:
        continue
    print(element)

Output:

4

5

6

Python continue Statement in nested loop

We can use the continue statement within a loop. Note that it doesn’t matter if that loop is within another loop or not! But be aware that the continue statement only breaks the current iteration of the enclosing loop and it doesn’t have any effect on the parent loop whatsoever.

Example: nested loop and continue statement

for element in range(4):
    print(f"Outer element: {element}")
    for el in range(10):
        if el <6:
            continue
        print(el)

Output:

Outer element: 0

6

7

8

9

Outer element: 1

6

7

8

9

Outer element: 2

6

7

8

9

Outer element: 3

6

7

8

9
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies