Python Continue

The Python Continue statement is another one to control the flow of loops. This Python Continue statement is used inside For Loop and While Loops. While executing these loops, if compiler find the python continue statement inside them, then compiler will stop the current iteration and starts the new iteration from the beginning.

For example we have 10 statements inside the loop. And we want to skip executing last 5 statements (statement 6 — statement 10) when 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 statement followed by Python continue statement. If the condition is True then it will stop executing statement 6 to 10 otherwise, it will execute statements 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 loop. The syntax of Python Continue Statement is

continue

Python Continue Statement in For Loop Example

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

# Python Continue Statement in For Loop example

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

In this python for loop continue example, it will ask the user to enter integer number and stores in number

number = int(input(" Please Enter any integer Value: "))

In the next line we used the For Loop with range. Please refer Python Break to understand the implementation of the Python break inside the For loop and While loop.

for i in range (1, number):

Inside the for loop we placed Python If Statement to test whether (i%2 != 0).

if(i%2 != 0):

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

For better understanding, we placed following print statement inside the If. So, whenever the iteration breaks then that value be printed from this print statement.

  print(" Odd Numbers = {0}(Skipped By Continue)".format(i))
  continue

If the condition is false then it will skip the continue statement for loop, and prints that number as output (In Our case Even Number) using the following print statement

print(" Even numbers = ", i)

Python Continue in While Loop Example

In this example we will show you, How to use Python Continue Statement inside the While Loop with example. This continue in while loop program will iterate from 1 to 10 and print every number up to 10. We also used if condition to skip few numbers.

# Python Continue Statement in While Loop example
i = 0
while(i <= 10):
    if (i== 5 or i == 9):
        print("Skipped Values =  ", i)
        i = i + 1
        continue
    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 the i value as 0 and next we used the While loop to check whether the i value is less than or equal to 10 or not.

Inside the While Loop we placed Python If to test whether i is equal to 5 or 9. If this condition is True then python continue statement will be executed. Next, the iteration will stop at that number without printing this:

print(" The Value of the Variable i is:  ", i)

For better understanding, we placed the following print statement inside the If condition. So, whenever the iteration breaks then that value will be printed as output.

print("Skipped Values =  ", i)

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

NOTE: We also used Arithmetic Operators + operator in this continue statement example to increment the i value (i = i + 1). If you forgot this line then you will end up in infinite loop.