Skip to content

Python Continue Statement | How works For, while Loop Example

  • by

Python Continue Statement is a loop control statement and mainly used to skip an iteration or ignore that condition. It works opposite to that of a break statement.

Where break statement is instead of terminating the loop and Continue Statement forces to execute the next iteration of the loop.

Python Continue Statement example

There are more loop control statements.

Syntax

 
continue

Examples:

The continue statement can be used in both while loop and for loop.

1. Python continue statement using in for loop

Below is the implementation of the above idea:

If condition statement with for-loop to check and skip if “a” word found.

for val in "Data":
    if val == "a":
        continue
    print(val)

print("The end")

Output:

D
t
The end

How continue statement works in Loop

Python continue statement in for loop

2. Python continue statement using in While loop

Using a while loop and if condition to skip a “2” number.

var = 5
while var > 0:
    var = var - 1
    if var == 2:
        continue
    print('Current variable value :', var)
print("Good bye!")

Output:

Current variable value : 4
Current variable value : 3
Current variable value : 1
Current variable value : 0
Good bye!

Do comment if you have any doubts and suggestions on this tutorial.

Note: This example (Project) is developed in PyCharm 2020.1 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.15.4
Python 3.7
All Python Programs are in Python 3, so it may change its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *