Python continue

The Python continue statement is another one to control the flow of loops. Like the Break statement, this Python continue statement is used inside For and While Loops. While executing these iterables, if the controller finds this statement inside them, the control will stop the current iteration and start the new iteration from the beginning.

For example, we have 10 statements inside the loop. And we want to skip executing the last 5 (statements 6 — 10) when a certain condition is True, or else it has to execute all the 10 statements inside a loop. In these situations, we place the condition after the 5th, followed by the Python continue statement. If the condition is True, it will stop executing statement6 to 10; otherwise, it will execute lines 1 to 10.

Python continue Syntax

We would like to share two examples to display the working functionality of the Python Continue statement in both For loop and While, and the syntax is


Python continue Statement in For Loop Example

How to use the Python continue Statement inside the For Loop? This program allows the user to enter any integer values. Next, it will display the Even and Odd numbers inside the integer value.

This Python for loop continue example will ask the user to enter an integer number and store it in numbers.

In the next line, we used the For Loop with range. Please refer to the Break code to understand the implementation of the Python break inside the For and While loop. We placed the If condition inside it to test whether (i%2 != 0).

If this condition is True, the Python continue statement in for loop will be executed. The iteration will stop at that number without printing the other print: print(” Even numbers = “, i).

For better understanding, we placed a print message inside the If. So, whenever the iteration breaks, that value is printed from this print function.

If the condition is false, it will skip this and prints that number as output (In Our case, an Even Number) using the following block of code.

number = int(input(" Please Enter any integer Value: "))
for i in range (1, number):
    if(i%2 != 0):
        print(" Odd Numbers = {0}(Skipped)".format(i))
    print(" Even numbers = ", i)
Please Enter any integer Value: 10
Odd Numbers = 1(Skipped)
Even numbers = 2
Odd Numbers = 3(Skipped)
Even numbers = 4
Odd Numbers = 5(Skipped)
Even numbers = 6
Odd Numbers = 7(Skipped)
Even numbers = 8
Odd Numbers = 9(Skipped)

Python continue in While Loop Example

In this example, we will show you how to use the Python continue statement inside the While Loop example. This program will iterate from 1 to 10 and print every number up to 10. We also used the if condition to skip a few numbers.

i = 0
while(i <= 10):
    if (i== 5 or i == 9):
        print("Skipped Values =  ", i)
        i = i + 1
    print(" The Value of the Variable i is:  ", i)
    i = i + 1
Python continue in While Loop

In this Python while loop continue program example, first, we initialized i value as 0, and next, we used the condition to check whether the i value is less than or equal to 10.

Inside the While Loop, we placed the If expression to test whether i is equal to 5 or 9. If this condition is True, then it will be executed. Next, the iteration will stop at that number without printing this line of code:

We placed the following print function inside the If condition for better understanding. So, whenever the iteration breaks by the Python continue statement, that value will be printed as output.

If the condition is false, then it will skip this and prints that number as output (In Our case, 0, 1, 2, 3, 4, 6, 7, 8, 10)

NOTE: We also used Arithmetic Operator + operator in this example to increment the i value (i = i + 1). If you forget this line, you will end up in an infinite loop.