Javaexercise.com

Continue Statement in Python

Python is a high-level, interpreted programming language, and is one of the most popular programming languages present in the software industry. One of the most important but often overlooked concepts in python is that of the continue statement. Let us have a look at them in detail.

What is continue statement in Python?

In python, continue statements are very much like their counterparts from other programming languages.

They are usually used inside loops for conditional (or even unconditional) skipping of execution of an iteration of the loop and to resume execution from the next iteration onwards.

In case the concept of loop nesting is involved, the execution of an iteration of the innermost loop is skipped, as per the location of the continue statement.

An advantage of the continue statement is that it can be used with both for and while loops in similar ways. To have a conditional skipping of loop iteration execution, continue statements are used with if statements and the desired conditions. The following flowchart would help us get a better understanding of the concept of continue statements -

flow char of continue statement in python

Syntax of continune statement in Python

The syntax of continue statements is very straightforward and is as follows. We simply use the keyword continue. 

loop(expression)
  continue

Continue statement with while loop in Python

The continue statements can be used in the body of a while loop as well. In this example, we shall consider a variable i which is assigned an initial value of 0.

The main purpose of the body of the while loop is to print the current value of i for an iteration and then increment it or increase it by 1.

The condition for the while loop to keep executing is i<3 which means that as long as the value of i is less than 3, the body of the while loop will keep getting executed.

Now, let us say we want to skip a particular iteration of this loop, specifically when the value of i becomes 1. For this, we use a conditional continue statement, that is, a continue statement with an if statement. The condition for the if statement becomes i==1.

Let us look at the python code and corresponding output for this example.

# Continue statement with while loop in  python
i=0
while i<3:
  if i==1:
    continue
  print(i)
  i += 1

Output

0

2

Continue statement with for loop example

Continue statements can be used in the body of a for loop as well. In this example, we shall consider the for loop to run from 0 to 2 which is specified using the range() function with a parameter 3.

The main purpose of the body of the for loop is to print the current value of i for an iteration and then increment it or increase it by 1.

Now, let us say we want to skip the execution of a particular iteration of this loop, specifically when the value of i becomes 1. For this, we use a conditional continue statement, that is, a continue statement with an if statement. The condition for the if statement becomes i==1.

Let us look at the python code and corresponding output for this example - 

# continue with  for loop in  python
for i in range(3):
  if i == 1:
    continue
  print(i)

Output

0

2

Conclusion

In this topic, we have learned the use and advantages of continue statements in a Python program, following a running example of a simple iterative printing program, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to [email protected] in case of any suggestions