Hit enter after type your search item

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.

See online demo and code

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:

See online demo and code

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:

See online demo and code

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 the numeric items and 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 will execute 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:

See online demo and code

continue nested for

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

This div height required for enabling the sticky sidebar