Python continue Statement

  • Python continue statement is used to skip the execution of the current iteration of the loop.
  • We can’t use continue statement outside the loop, it will throw an error as “SyntaxError: ‘continue’ outside loop“.
  • We can use continue statement with for loop and while loops.
  • If the continue statement is present in a nested loop, it skips the execution of the inner loop only.
  • The “continue” is a reserved keyword in Python.
  • Generally, the continue statement is used with the if statement to determine the condition to skip the current execution of the loop.

Flow Diagram of the continue Statement

Continue Statement Flow Diagram
Python continue Statement Flow Diagram

Python continue Statement Syntax

The continue statement syntax is:

continue

We can’t use any option, label or condition with the continue statement.


Python continue Statement Examples

Let’s look at some examples of using the continue statement in Python.

1. continue with for loop

Let’s say we have a sequence of integers. We have to skip processing if the value is 3. We can implement this scenario using for loop and continue statement.

t_ints = (1, 2, 3, 4, 5)

for i in t_ints:
    if i == 3:
        continue
    print(f'Processing integer {i}')

print("Done")

Output:

Python Continue Statement For Loop
Python continue Statement with for Loop

2. continue statement with the while loop

Here is a simple example of using continue statement with the while loop.

count = 10

while count > 0:
    if count % 3 == 0:
        count -= 1
        continue
    print(f'Processing Number {count}')
    count -= 1

Output:

Python Continue Statement While Loop
Python continue Statement with while Loop

3. continue statement with a nested loop

Let’s say we have a list of tuples to process. The tuple contains integers. The processing should be skipped for below conditions.

  • skip the processing of tuple if its size is greater than 2.
  • skip the execution if the integer is 3.

We can implement this logic with nested for loops. We will have to use two continue statements for implementing above conditions.

list_of_tuples = [(1, 2), (3, 4), (5, 6, 7)]

for t in list_of_tuples:
    # don't process tuple with more than 2 elements
    if len(t) > 2:
        continue
    for i in t:
        # don't process if the tuple element value is 3
        if i == 3:
            continue
        print(f'Processing {i}')

Output:

Continue Statement With Nested Loop
Python continue Statement with Nested Loop

Why Python doesn’t support labeled continue statement?

Many popular programming languages support a labeled continue statement. It’s mostly used to skip the iteration of the outer loop in case of nested loops. However, Python doesn’t support labeled continue statement.

PEP 3136 was raised to add label support to continue statement. But, it was rejected because it’s a very rare scenario and it will add unnecessary complexity to the language. We can always write the condition in the outer loop to skip the current execution.


Python continue vs break vs pass

continuebreakpass
The continue statement skips only the current iteration of the loop.The break statement terminates the loop.The pass statement is used to write empty code blocks.
We can use continue statement only inside a loop.We can use break statement only inside a loop.We can use pass statement anywhere in the Python code.