×

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

Kite Python

Kite in Python: The Kite is a package provided by the python programming language; it works with the help of artificial intelligence and helps us write code inside the visual studio....

3 minutes read.

Python abs() function

Python abs() function The abs() function returns the absolute value of a number. Syntax abs(x) Parameter x: The parameter ‘x’ can be an integer value, a floating point number or a complex number. Return This function returns...

1 minute read.

How to run a Program in Python

How to run a Program in Python Writing a program in Python is an easy task, beginners who are ready to kickstart their career in the world of programming can create...

3 minutes read.

tell() function in Python

Introduction The tell() function in Python is used to return the current position of the file read/write pointer within the file. An integer value is returned by this method, and it...

2 minutes read.

Python program to print calendar

Python program to print the calendar This article will explain how to print the calendar of month and year using Python's calendar module. It's a straightforward thing in Python by importing...

1 minute read.

Python Pickle

The pickle is a module that enables serialization and de-serialization of the structure of the object in python. Pickling is the process that uses the protocols to convert the Python...

5 minutes read.

Python program to count and display vowels in a string

Python program to count and display vowels in a string This python program counts the vowels in a string and displays them on the screen. We can do this in several...

2 minutes read.

__GETITEM__ and __SETITEM__ in Python

These methods are used in assignment operations, unary comparison operations, binary comparison operations and binary operations. These are pre-defined methods that perform many operations on a class instance. Examples like...

3 minutes read.

Iterate a Dictionary in Python – Part 2

In this tutorial, we will learn above various methods used to Iterate a Dictionary in Python. Dictionary: In Python, a dictionary is an unordered collection of data values that is used to...

3 minutes read.

Python Syntax Error Invalid Syntax

Python is renowned for its simple and direct syntax. However, we might come across some things that Python doesn't allow if we are learning Python for the very first time or if...

14 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.

How to print in the same line in Python?

How to print in the same line in python By default, the print function in Python takes us to the next line and prints the desired statement in the output. In this...

5 minutes read.

Python Typing Module

An Introduction to the Typing Module The typing module is introduced in Python version 3.5 and used to provide hinting method types in order to support static type checkers and linters...

10 minutes read.

Python elif

Python elif The elif statement is used to check multiple conditions and execute the specific block of statements depending upon the true condition among them. Syntax if expression1: statement elif expression2: statement elif expression3: statement else: statement The elif statement can be optional...

3 minutes read.

How to Sort a String in Python?

The characters in the string are sorted or put in alphabetical order using the sort string function in Python. Python has built-in techniques for sorting strings available. Since we occasionally...

6 minutes read.

Python len() function

Python len() function The len() function in Python  returns the number of items in an object. Syntax len(s) Parameter s: This parameter represents a sequence (such as a string, bytes, tuple, list, or range) or...

1 minute read.

Python program to check if two strings are anagram or not

Python program to check if two strings are anagram or not Problem: This is a python program that takes two strings and checks if given strings are anagram or not. Examples: Input: string1...

1 minute read.

Python Unit Test Cheat String

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.

wxPython Panel class

wxPython Panel class The Widgets which is shown in the frame of GUI window such as text box, buttons, static text etc. are put inside the panel class of the wxpython...

2 minutes read.

Create a Table Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

3 minutes read.