What is Python continue statement?

The continue statement is used in the for and while loops in Python to omit the execution for the current iteration. Any statements after the continue statement within the loop will be neglected and execution moves back to the next iteration.

An example of continue statement in for loop

In the following example, a list of five numeric values is created. This is followed by using a for loop to iterate through the list items. The list items are:

lst_cont = [5,10,15,20,25]

Inside the for loop, an if statement is used and the current value of the item is checked. As the current value reaches 15, the continue statement will execute that will neglect the next print function, so 15 should not display.

#A Demo of continue statement in for loop

lst_cont = [5,10,15,20,25]


for z in lst_cont:

    if z == 15:

        continue

    print ("The current value of z = " , z)

continue for loop

Displaying only leap years from 1900 to 2021 example

In this demo, a range is created that starts from 1900 and ends at 2021. The range will move with the default step value of 1. In the for loop, the current year is displayed by using the print function. However, an if statement is used to check whether the current value is a leap year or not.

If this is a leap year, it will display the value, otherwise, continue statement will execute that will omit the print function and execution moves to next range value:

#A Demo of continue statement with range and for loop

for curr_yr in range(1900, 2021):

   if curr_yr % 4 == 0:

      print("Leap Year", curr_yr)

      continue

Python continue

You can see, the continue Python statement omitted the print function every time the current range value is a non-leap year i.e. not dividable by 4.

The while loop example with continue statement

Similarly, you may use the continue statement in the while loop for omitting the next lines after the continue statement and moving the execution to the next iteration.

The following example shows using the Python continue statement in while loop. Only the even numbers are displayed from 1-20. The while loop iterates through number 1 to 20 by incrementing 1 in each iteration. As an odd number is found, the continue statement will neglect the print function and moves the execution to next number and so on:

#Example of continue in while loop

y = 1

while y <= 20:             

   y = y + 1

   if y%2 != 0:

      continue

   print ("The even Number :", y)

continue while

You can see, only even numbers are displayed.

What is the scope of continue if used in nested for loop?

The scope of the continue statement is nearest enclosing loop. Let us say, you have a nested loop. If you place the continue statement inside the second for loop, then after execution it will omit any statements inside the second loop and iteration moves to the next element of the second or inner loop.

This is demonstrated in the example below where continue statement is placed inside the inner loop.

Two lists are created: one with numeric items and the other with characters. The outer for loop iterates through the numeric list with items [1, 2, 3]. In each iteration, the inner for loop will iterate through the second list with items [‘a’, ‘b’, ‘c’].

Inside the inner loop, a print function is used to display the list items; so it displays a, b, c in each iteration. However, an if statement is used to check the current item value and if it is equal to ‘b’ the continue statement executes that will omit the print function. So, only the letter ‘a’ and ‘c’ should display in each iteration of the outer loop. Have a look at the code and output:

lst_numbers = [1, 2, 3]

lst_chars = ['a', 'b', 'c']



for num in lst_numbers:

    print("The outer loop number: ",num)

    for str_char in lst_chars:

        if str_char == 'b':

            continue

        print(str_char)

continue nested for

You can see, the continue statement only impacted the inner for loop while the outer loop executed normally.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!