Python Continue Statement

In python, continue statement is useful to skip the execution of the current iteration of the loop and continue to the next iteration.

 

The main difference between break and continue statements are the break statement will completely terminate the loop, and the program execution will move to the statements after the body of the loop, but the continue statement skips the execution of the current iteration of the loop and continue to the next iteration.

Continue Statement Syntax

Following is the syntax of defining the continue statement in python.

 

continue

Continue Statement Flow Chart

Following is the flow chart diagram of the continue statement in python.

 

Python continue statement flow chart diagram

 

If you observe the above continue statement flow chart diagram, the execution of code inside a loop will be skipped for a particular iteration when the continue statement is executed.

For Loop with Continue Statement

In python, you can use the continue statement in for loop to skip the current iteration of the loop and continue to the next iteration.

 

Following is the example of using the continue statement in python for loop to skip the execution of a particular iteration and pass control to the next iteration of the loop based on the defined condition.

 

for x in range(5):
  if x == 3:
    continue
  print(x)
print("Outside of the loop")

When you execute the above python program, you will get the result as shown below.

 

0
1
2
4
Outside of the loop

If you observe the above result, the for loop has skipped the execution for 3rd item iteration and continued to the next iteration.

While Loop with Continue Statement

Like python for loop, you can also use continue statement in a while loop to skip the execution of the current iteration of the loop and continue to the next iteration.

 

Following is the example of using a continue statement in python while loop to skip the execution of a particular iteration and pass control to the next iteration of the loop based on the defined condition.

 

a = 0
while a < 5:
  a += 1
  if a == 3:
    continue
  print(a)
print("Outside of Loop")

When you execute the above python program, you will get the result as shown below.

 

1
2
4
5
Outside of Loop

If you observe the above result, the while loop has skipped the 3rd item iteration execution and continued to the next iteration.

 

This is how you can use continue statement in python for, and while loops to skip the execution of a particular iteration and pass control to the loop's next iteration based on your requirements.