×

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

Python List sort() method

The list.sort () method in Python sorts the items of the list in place. Syntax list.sort(key=None, reverse=False) Parameter reverse: If a Boolean value ‘True’ is passed, the  sorting will be done in the descending order else for ‘False’...

2 minutes read.

Python Tuple Methods

Python Tuple Methods Python has two built-in methods that are used for tuples. The following are the two methods: Method Description count() The tuple.count() method in Python returns the number of times a...

1 minute read.

Python Program to find the length of a string

Python program to find the length of a string A string in Python is a set of Unicode characters. It can't be changed once it's been declared. The number of characters...

2 minutes read.

Creating Web Application in python

Web Application :  Web Application is an application software that runs in web browser . Software programs will run on operating system. Whereas Web Applications will run on World Wide Web...

5 minutes read.

os.stat() method in Python

In Python, the os module provides the capacity to interact with the operating system. The operating system comes under the Python module. In this module, Python provides a specific feature...

3 minutes read.

Python round() function

Python round() function The round() function in Python returns a number rounded to ‘ndigits’ precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input. Syntax round(number[, ndigits]) Parameter Number: This parameter represents the...

1 minute read.

Writing to a CSV file in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

6 minutes read.

Python whois

What is whois? Whois is a protocol used to identify the owner of the registered domain name. It is a querying database that is used to record the registered users. WHOIS is...

4 minutes read.

Python IDE

What is an IDE?  An IDE or an Integrated Development Environment is a type of software where developers can develop many software’s and applications and run the programs inside it. An...

11 minutes read.

Python program to perform the arithmetic operation

Python program to perform the arithmetic operation This program will write a code to perform some basic arithmetic operations like addition, subtraction, multiplication, exponent, modulus, and division. Here we first need...

2 minutes read.

Imread Python

In this walkthrough, we'll go over the specifics of using the imread() method of OpenCV-Python and various methods for loading images. Imread is an Image reading library. One of the most helpful...

7 minutes read.

Subprocess in Python

In this tutorial, we will understand what is a subprocess in python and will understand how to use it. Subprocess A subprocess is a very prevailing portion of the python library. It...

3 minutes read.

Best Database for Python

Database The collection of structured data or information in an organized format in a computer system is known as Database. The data is inserted, deleted, updated, controlled, or manipulated in a...

6 minutes read.

Difference between Mutable and Immutable Objects

Difference between Mutable and Immutable Objects As we all know that Python is an object-oriented programming language. Object-oriented programming approach is based on the objects and the classes’ stores the data...

4 minutes read.

Accuracy_score Function in Sklearn

A crucial stage in data science is measuring our model's performance using the appropriate metric. In this article, we will examine two methods for calculating the accuracy of your predictions:...

13 minutes read.

Returning Multiple Values in Python

Python is considered a general-purpose programming language; it is a high-level programming language that is not much difficult and easier to learn. It is rich in libraries that can be...

3 minutes read.

Filter List in Python

Python: Python is one of the most used programming languages, as it is used widely in software and data analysis, web development, etc. It is said to be a user-friendly programming...

3 minutes read.

How to make a firewall in Python?

Firewall: The firewall is a network which controls the incoming and outgoing network traffics of a monitor. It blocks the dataset based on the set of rules written in the security...

3 minutes read.

Create the First GUI Application using PyQt5 in Python

GUI: A graphical user interface, or GUI, is present on most personal computers. It provides a simple experience for individuals with various computing skill levels. GUI apps may take more resources...

3 minutes read.

ModuleNotFoundError No module named 'mysql' in Python

mysql module not found is an error that occurs in python. It appears like ModuleNotError: No module named 'mysql.' This error occurs when you forget to install a module called...

3 minutes read.