Python Continue Statement

Continue in Python is a loop control statement similar to the break statement. It breaks the current execution (iteration) of loop and transfers the program control back to the beginning of the loop for the next execution without exiting the loop.

In other words, the continue statement tells the Python to stop the current execution of loop and to begin a new execution of loop.

When the continue statement encounters in a loop, the remaining statements in the current iteration skips (i.e. not executed) in a specified condition, and the control of execution continues with the next repetition of the loop.

In simple words, when encountered, the control jumps to the beginning of the loop without executing the remaining statements in the current iteration and continues with the next iteration. We can use continue statement in both while and for loops.

Syntax of Continue Statement in Python


The general syntax of continue statement is just as simple as ‘break statement’. It is as:

continue

Here, continue is a keyword.

Let’s take an example program to understand the concept of continue statement in Python.

for letter in 'Technology':
    if letter == 'n':
        print('Skipping the loop at', letter)
        continue
    print(letter)


When you will run the above code, the following result will produce at the console.

Output:
      T
      e
      c
      h
      Skipping the loop at n
      o
      l
      o
      g
      y

As you can see in the above output, the continue statement does not break out of the loop entirely. It just jumps back to the beginning of the loop by skipping the rest of code in the loop body for the next iteration without exiting the loop.

Look at the below figure to understand more clearly the continue statement in the for loop.

Python continue statement example

Flowchart Diagram of Continue Statement in Python


Look at the below flowchart diagram of the continue statement in Python.

Flowchart of continue statement in Python

Example Program based on Continue Statement


Example 1:

Let’s write a program in Python to use the continue statement inside the for loop.

for x in range(1, 6):
    if x == 3:
        print('Continue without 3')
        continue # It will skip the rest iteration and go back in the for loop with the next iteration.
    print(x)
Output:
      1
      2
      Continue without 3
      4
      5

As you can see in the output, 3 is not displayed on the console because when x == 3, the loop is continued and redirected to the control of execution back to the next iteration of the loop. As a result, the value 3 of x will not print.


Example 2:

Let’s write a program in Python to use the continue statement inside the while loop.

# Displaying numbers using continue statement.
# while loop.
x = 1 # Initialization.
while x <= 6:
    if x == 3:
        x = x + 1
        continue
    print(x)
    x = x + 1
Output:
      1
      2
      4
      5
      6

As you can see in the output, the number 3 is skipped when x is equal to 3.


Example 3:

Let’s write a program to use the continue statement inside the nested for loop.

# Outer loop.
for x in range(1, 4):
   # Inner loop.
     for y in range(1, 4):
       if x == 2 and y == 3:
           continue # continue statement inside the inner for loop.
       print(x,y)
Output:
      1 1
      1 2
      1 3
      2 1
      2 2
      3 1
      3 2
      3 3

Example 4:

for x in range(1, 7):
    if x < 2:
        continue
    print(x)
    if x < 4:
        continue
    print(10 * x)
Output:
      2
      3
      4
      40
      5
      50
      6
      60

In this program code, we have created a loop that iterates the value of variable x from 1 to 6. If x is less than 2, then the loop is continued and do nothing.

Once x is 3 or higher, Python prints the values. Once x is greater than 5, Python prints 10 * x. So, the above program code will print the values as shown in the output.

Difference between Break and Continue in Python


Both break and continue are loop control statements that change from its normal sequence. We can use both in for, while, and nested loops. But there are also a few basic differences between them. They are as follows:

1. Break statement is used to terminate or break the loop, whereas continue statement is used to escape from the current iteration.

2. Break statement transfers the control to the statement (if any) immediately following the loop. On the other hand, continue transfers the control back to the start of the loop for the next iteration.


In this tutorial, you have learned about continue statement in Python with basic example programs. Hope that you will have understood the basic concept of continue statement and practiced all example programs. Stay tuned with the next tutorial, you will learn about pass statement in Python with the help of basic examples.
Thanks for reading!!!