×

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

Isreal() Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

4 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 Configure Python Interpreter in Eclipse

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.

Python Parallel Processing

By performing more jobs concurrently, your software may complete more tasks in a shorter amount of time. These aid in solving major issues. The following subjects will be covered in...

2 minutes read.

Python String rstrip() method

Python String rstrip() method The string.rstrip() method in Python returns a copy of the string with trailing characters removed. Syntax string.rstrip([chars]) Parameter chars:  This argument represents a string specifying the set of characters to be...

1 minute read.

Python Program for Tower of Hanoi

In this tutorial, we will examine and learn about the Python coding used to create the video game Tower of Hanoi. Before seeing the game's step-by-step implementation in Python, we...

3 minutes read.

Python Scikit-image | Image Processing Using Scikit-Image

What is Image Processing? The world is defined with images and, every image has its different specialties. An image can contain much-needed information that can be helpful in various ways. The process...

4 minutes read.

How to upgrade PIP in python

We use the PIP command in our command prompt to install any package. PIP is used to install published Python packages in the PyPI (Python package index) or other indices....

3 minutes read.

_name_ in Python

Introduction: The code at level 0 indentation is to be performed when the command to run a Python program is supplied to the interpreter because Python does not have a main() function...

4 minutes read.

Android apps for coding in python

Nowadays, it is the generation of smartphones. The world can be seen and acknowledged with a single click on your mobile phone. Everyone uses mobile phones to do any work,...

9 minutes read.

Insertion Sort using Python

Insertion sort is a type of sorting technique that is used for sorting an array with random elements. Using sorting methods, any unsorted array can be sorted into ascending or...

3 minutes read.

Python coding platform

Python is a popular general-purpose programming language with many applications. High-level data structures, datatypes, dynamic binding, and many other features make it useful for both designing complex applications and "glue...

6 minutes read.

Python vs R

Python: - Python is a popular high-level, general-purpose programming language. Python (the most recent version is Python 3) is a programming language used in the software industry for website development, machine...

3 minutes read.

Python ascii() Function

Python ascii() Function The ascii() function returns a readable version of any object (Strings, Tuples, Lists, etc). This function will replace any non-ascii characters with escape characters. Syntax ascii(object) Parameter object:  An object, like String, List, Tuple, Dictionary, etc. Return This...

1 minute read.

Self in Python

 “self" is neither a keyword nor has a special meaning in Python, but it has a place and a job to do in Object-oriented programming. When we create a class...

6 minutes read.

Python CGI Programming

The Concept of CGI CGI is an abbreviation for Common Gateway Interface. It is not a type of language but a set of rules (specification) that establishes a dynamic interaction between...

9 minutes read.

Python PPTX

Python-pptx is a python library that enables the user to create and update PowerPoint (.pptx) presentations. It is used to create modified PowerPoint presentations from the db content that can...

10 minutes read.

Python Functionalities of Pillow Module

This instructional exercise is about "Pillow" library, one of the significant libraries of python for picture control. Pillow library of python, is an easy to use and also open source...

14 minutes read.

Python Memory Management

In order to manage memory, Python uses a private heap that contains all of its data structures and objects. The Python memory manager is responsible for the internal management of...

11 minutes read.

GUI Calendar using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.