continue Statement in Python

This tutorial will go over one of the most common conditional statements in Python, the "continue" statement. The "continue" statement can be useful in creating a logical program. Therefore, I've provided all the details about it along with its examples. This article deals with:

What exactly is a "continue" statement?

The continue statement or keyword is also a conditional statement like break and pass. The continue statement is used to skip the execution of the remaining part (statement(s)) of the current  loop and continue for the next iteration.

Syntax of the "continue" statement

The syntax to use the continue statement is the continue keyword itself. That is, we do not need anything else attached to the statement. Here is its syntax:

continue

Examples of the "continue" statement

Because you're learning the concept of computer programming in Python. Therefore, without an example program, how can it be possible to complete the thing?
Let's start with a very simple example of a continue statement:

nums = [1, 2, 3, 4, 5]
for n in nums:
    if n == 2:
        continue
    print(n)

python continue statement

As you can see, the number 2 does not get printed on the output. Because when the condition "n==2" satisfies or evaluates to be true, that is, when the value of n becomes equal to 2, then program flow goes inside the if's body and executes the "continue" keyword, which skips the remaining part of the loop's statement(s) to be executed and continues for the next iteration.

Let's take another example that, of course, uses the continue statement in a while loop:

count = 0
while count<10:
    count = count+1
    if count>5:
        continue
    print(count)

The above program produces:

continue statement in python

From the above program, the statement:

count = count+1

executes 10 times, whereas the statement:

print(count)

executes only five times. This is because when the value of count becomes greater than 5, every time the condition of the "if" "count>5" evaluates to true, using the continue keyword, the next iteration of the loop gets executed, and the print() statements get skipped. Here's another program that may dispel any doubts you have about the previous one:

count = 0
while count<10:
    count = count+1
    print("\nAbove \"continue\" Keyword")
    if count>5:
        continue
    print("Below \"continue\" Keyword", count)

The snapshot given below shows the sample output produced by the above Python program:

continue keyword in python

As you can see, the second print() statement inside the loop does not get executed when the value of count is 6, 7, 8, 9, or 10.

Let's take a program that provides the master use of the "continue" keyword in a program. This is just a hint as to how you can use the continue keyword or statement in your Python program or project to create this type of amazing logic. This is the same program with different logic as the program created in the "break" keyword post.

num = 2
while num>1:
    mul = 1
    print("\n---Table of", num, "---")
    while mul<=10:
        print(num, "x", mul, "=", num*mul)
        mul += 1
    print("\nWant to continue printing Table ? (y/n) ", end="")
    confirm = input()
    if confirm == 'y':
        num = num+1
        continue
    else:
        num = 0

The initial output produced by the above program is shown in the snapshot given below:

continue statement python example program

Now to continue printing the table, enter y; otherwise, enter anything else, like "n." Here is another sample run with user input "y" to print another table like the one shown in the snapshot given below:

python continue statement example

And here is one more sample run, but this time with user input n:

python continue keyword

Now you may think, "What is new in the above program?"
The main thing to point out from the above program is that we've continued printing the table using the continue statement or keyword but stopped printing the table without using the break keyword.

That is, when the user enters y as input, the condition confirm == "y" evaluates to be True, and program flow goes inside the if's body and the value of num  gets incremented by 1, then using continue, the next iteration of the loop continues.

And when the user enters n (anything other than y) as input, then the condition "confirm == 'y'" evaluates to false, and the program flow goes inside the "else" body and 0 gets initialized to num. Because all of the loop's statements are executed, the program flow evaluates the condition "num>1" once more. However, the condition "num>1" or "0>1" evaluates to false this time. Therefore, the execution of the loop gets stopped or terminated.

Python Online Test


« Previous Tutorial Next Tutorial »