Python continue Statement

The continue statement, also borrowed from C,It is used to continue the loop with the next iteration of the loop.


Example of continue statement

Example : continue statement in for loop


# example of continue statement
for i in range(1, 4):
    print("First statement")
    print("Second Statement")
    print("value of i is " + str(i))
    if (i == 2):
        print("going to skit the below line for this iteration")
        continue
    print("Third Statement")
    print("Fourth Statement")
    print("Fifth Statement")
    print("=============================")
First statement
Second Statement
value of i is 1
Third Statement
Fourth Statement
Fifth Statement
=============================
First statement
Second Statement
value of i is 2
going to skit the below line for this iteration
First statement
Second Statement
value of i is 3
Third Statement
Fourth Statement
Fifth Statement
=============================

continue statement skip the current iteration and continue with next one

Another simple example of continue statement


for num in range(2, 10):
    if num % 2 == 0:
        print("Found an even number", num)
        continue
    print("Found a number", num)
Found an even number 2
Found a number 3
Found an even number 4
Found a number 5
Found an even number 6
Found a number 7
Found an even number 8
Found a number 9