×

Python Continue Statement

In Python, loops automate and repeat processes in a cost-effective manner. However, there may be occasions when you wish to entirely exit the loop, skip an iteration, or ignore the condition. Loop control statements can be used to do this. Continue is a loop control statement that allows you to change the loop's flow.

The continue statement is a loop control statement that forces the loop to execute the next iteration while skipping the rest of the code inside the loop for the current iteration only. When the continue statement is executed in the loop, the code inside the loop following the continue statement is skipped for the current iteration only, and the loop's next iteration begins.

Let us look at some examples to understand the concept better.

Consider the following scenario: you need to develop a programme that prints numbers from 1 to 10, but not 6. It is mentioned that you must perform this with a loop, and that you may only use one loop. Here's when the continue statement comes into play. What we can do here is run a loop from 1 to 10 and compare the value of the iterator with 6 every time. If it equals 6, we'll use the continue statement to skip to the next iteration without writing anything; otherwise, the value will be printed.

# Python program to

# demonstrate continue

# statement

# loop from 1 to 10

for i in range(1, 11):




    # If i is equals to 6,

    # continue to next iteration

    # without printing

    if i == 6:

        continue

    else:

        # otherwise print the value

        # of i

        print(i, end=" ")

Output:

1 2 3 4 5 7 8 9 10

The continue statement, also borrowed from C just like the break statement, continues with the next iteration of the loop:

>>> for num in range(2, 10):

...     if num % 2 == 0:

...         print("Found an even number", num)

...         continue

...     print("Found an odd number", num)

Output:

Found an even number 2

Found an odd number 3

Found an even number 4

Found an odd number 5

Found an even number 6

Found an odd number 7

Found an even number 8

Found an odd number 9

Let’s see how the python continue statement works with different loops in the program.

Continue with for loop

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

for i in t_ints:

    if i == 3:

        continue

    print(f'Processing integer {i}')


print("Done")

Output:

Processing integer 1

Processing integer 2

Processing integer 4

Processing integer 5

Done

Continue with a while loop

count = 10

while count > 0:

    if count % 3 == 0:

        count -= 1

        continue

    print(f'Processing Number {count}')

    count -= 1

Output:

Processing Number 10

Processing Number 8

Processing Number 7

Processing Number 5

Processing Number 4

Processing Number 2

Processing Number 1

Continue with a nested loop

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:

Processing 1

Processing 2

Processing 4

Related Topics

Operator Overloading in Python

In any programming language, + is an arithmetic operator; it adds two numbers to give the sum. Have you ever tried to concatenate two strings? Concatenating two strings is adding...

5 minutes read.

Python Program to check whether a given number is Armstrong or not

Program to check whether a given number is Armstrong or not A number is said to be an Armstrong if the sum of each digit's cube of a given number equals...

1 minute read.

Check Letter in a String Python

Checking a specific letter or a character in a given string or a string which is taken as an input from the user is possible in Python in broadly three...

3 minutes read.

Explain sklearn clustering in Python

Make a connection and patterns across datasets by using clustering, one of the unsupervised machine learning approaches. Grouping is crucial because it ensures unlabelled data's natural clustering. The sample from...

7 minutes read.

How to import pandas in python

How to Install Pandas in Python Python is a vast ocean of libraries, modules, and different functions. It has a solution for almost everything. Using Python, we can simplify even a...

3 minutes read.

Python String Variable

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

4 minutes read.

Python Dictionary items() method

Python Dictionary items() method The dictionary.items() method in Python returns a view object that displays a list of dictionary's (key, value) tuple pairs. Syntax dictionary.items() Parameter NA Return This method returns a view object, displaying the list...

1 minute read.

Cross Entropy in Python

Introduction Cross-entropy loss is frequently combined with the softmax function. Determine the total entropy among the distributions or the cross-entropy, which is the difference between two probability distributions. For the purpose...

5 minutes read.

How to Concatenate Two Strings in Python

How to Concatenate Two Strings in Python Like the other data types, operations on strings are quite useful when we deal with some real-life applications. Here we will talk about a simple...

4 minutes read.

Face Recognition in Python

In this tutorial, we will understand what is face recognition and how it is achieved in python. Face recognition is of great utility in real-world scenarios. It is an extended step...

3 minutes read.

Python Dictionary Methods

Python Dictionary Methods Python has a set of built-in methods that dictionary objects can call. All the python dictionary methods are as follow: Methods Description clear() The dictionary.clear() method removes all the elements...

3 minutes read.

Poolmanager in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Yield vs Return in Python

In this article, you will learn about " Yield " and " Return " in Python. You will also understand the difference between " Yield " and " Return "...

6 minutes read.

Raise Exception in Python

Most of the programmers/developers create large programs to solve complex tasks in Python. The code can be of 1000 lines and even more based on the need of the problem....

5 minutes read.

Python Uses

Python has advanced in recent years to rank among the programming languages that are most often used globally. It is utilized in everything, including machine learning, software testing, and website...

4 minutes read.

SKLearn Linear Module

The SK learn linear module is one such module that helps to study the relationship between the independent and dependent variables.The linear module can be implemented by using the best...

3 minutes read.

Python math.cos and math.acos function

Math.cos() function In Python, the Math module is used for performing the mathematical operations. It includes the math.cos() function that is used for obtaining the cosine value of an angle in...

3 minutes read.

Python Project Ideas

One of the most widely used programming languages today is Python. This pattern appears set to continue through 2023 and beyond. Therefore, working on some current Python project ideas is the...

10 minutes read.

How to get current date in python?

How to get current date in python? In Python programming language, date and time are not just a data form, but it is possible to import a method called datetime to operate...

3 minutes read.

Unicode to String in Python

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.