Python continue statement

The continue statement is used in the for and while loops in Python to omit 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)

Output:

continue for loop

Displaying only leap years from 1900 to 2021 example

  • In this example, a range is created that starts from 1900 and ends in 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 that 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 the continue statement

Similarly, you may use the continue statement in the while loop to omit the next lines after the continue statement and move 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 move the execution to the 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)

Result:

The even Number: 2
The even Number: 4
The even Number: 6
The even Number: 8
The even Number: 10
The even Number: 12
The even Number: 14
The even Number: 16
The even Number: 18
The even Number: 20

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 the 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 the continue statement is placed inside the inner loop.

In the example:

  • 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)

Output:

The outer loop number: 1
a
c
The outer loop number: 2
a
c
The outer loop number: 3
a
c

You can see that 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 solve the mysteries of coding together!