Open In App

Python Continue Statement

Last Updated : 09 May, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Python Continue Statement skips the execution of the program block after the continue statement and forces the control to start the next iteration.

Python Continue Statement

Python Continue statement is a loop control statement that forces to execute the next iteration of the loop while skipping the rest of the code inside the loop for the current iteration only, i.e. when the continue statement is executed in the loop, the code inside the loop following the continue statement will be skipped for the current iteration and the next iteration of the loop will begin.

Python continue Statement Syntax

while True:
    ...
    if x == 10:
        continue
    print(x)

Flowchart of Continue Statement

Python Continue Statement

flowchart of Python continue statement

Continue statement in Python Examples

Demonstration of Continue statement in Python

In this example, we will use continue inside some condition within a loop.

Python3




for var in "Geeksforgeeks":
    if var == "e":
        continue
    print(var)


Output:

G
k
s
f
o
r
g
k
s

Explanation: Here we are skipping the print of character ‘e’ using if-condition checking and continue statement.

Printing range with Python Continue Statement

Consider the situation when you need to write a program that prints the number from 1 to 10, but not 6. 

It is specified that you have to do this using a loop and only one loop is allowed to use. Here comes the usage of the continue statement. What we can do here is we can run a loop from 1 to 10 and every time we have to compare the value of the loop variable with 6. If it is equal to 6 we will use the continue statement to continue to the next iteration without printing anything, otherwise, we will print the value.

Python3




# 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 

Note: The continue statement can be used with any other loop also like the while loop, similarly as it is used with for loop above.

Continue with Nested loops

In this example, we are creating a 2d list that includes the numbers from 1 to 9 and we are traversing in the list with the help of two for loops and we are skipping the print statement when the value is 3.

Python3




# prints all the elements in the nested list
# except for the ones with value 3
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
 
for i in nested_list:
    for j in i:
        if j == 3:
            continue
        print(j)


Output 

1
2
4
5
6
7
8
9

Continue with While Loop

In this example, we are using a while loop which traverses till 9 if i = 5 then skip the printing of numbers.

Python3




# prints the numbers between
# 0 and 9 that are not equal to 5
i = 0
while i < 10:
    if i == 5:
        i += 1
        continue
    print(i)
    i += 1


Output 

0
1
2
3
4
6
7
8
9

Usage of Continue Statement

Loops in Python automate and repeat tasks efficiently. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by loop control statements. Continue is a type of loop control statement that can alter the flow of the loop. 

To read more on pass and break, refer to these articles:

  1. Python pass statement
  2. Python break statement


Next Article

Similar Reads

Loops and Control Statements (continue, break and pass) in Python
Python programming language provides the following types of loops to handle looping requirements. Python While Loop Until a specified criterion is true, a block of statements will be continuously executed in a Python while loop. And the line in the program that follows the loop is run when the condition changes to false. Syntax of Python Whilewhile
4 min read
Difference between continue and pass statements in Python
Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by loop control statements. Loop control statements change execution from its normal sequence. When execution leaves a scop
3 min read
Python EasyGUI – Continue Cancel Box
Continue Cancel Box : It is used to display a window having a two option continue or cancel in EasyGUI, it can be used where there is a need to display two option continue or cancel for example when we want to confirm the option if continue is pressed application will move forward else it will get terminated, below is how the continue cancel box lo
3 min read
break, continue and pass in Python
Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by loop control statements. Loop control statements change execution from their normal sequence. When execution leaves a sc
7 min read
PyQt5 QCalendarWidget - Continue functions by enabling
In this article, we will see how we can continue the functions of the QCalendarWidget. Stopping function means that the QCalendarWidget will not be able to do a single thing like selection changing date etc. Disabling the calendar do not delete or hide the widget from the screen. Disabling is used to stop the user from doing any changes on it, but
2 min read
Using Else Conditional Statement With For loop in Python
Using else conditional statement with for loop in python In most of the programming languages (C/C++, Java, etc), the use of else statement has been restricted with the if conditional statements. But Python also allows us to use the else condition with for loops. The else block just after for/while is executed only when the loop is NOT terminated b
2 min read
Statement, Indentation and Comment in Python
Here, we will discuss Statements in Python, Indentation in Python, and Comments in Python. We will also discuss different rules and examples for Python Statement, Python Indentation, Python Comment, and the Difference Between 'Docstrings' and 'Multi-line Comments. What is Statement in Python A Python statement is an instruction that the Python inte
7 min read
How to Use IF Statement in MySQL Using Python
Prerequisite: Python: MySQL Create Table In this article, we are going to see how to use if statements in MySQL using Python. Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database from Python. MySQL Connector-Python module is an API in python for communicating with
2 min read
How to Execute a SQLite Statement in Python?
In this article, we are going to see how to execute SQLite statements using Python. We are going to execute how to create a table in a database, insert records and display data present in the table. In order to execute an SQLite script in python, we will use the execute() method with connect() object: connection_object.execute("sql statement") Appr
2 min read
Python pass Statement
The Python pass statement is a null statement. But the difference between pass and comment is that comment is ignored by the interpreter whereas pass is not ignored. The Syntax of the pass statementpassWhat is pass statement in Python? When the user does not know what code to write, So user simply places a pass at that line. Sometimes, the pass is
3 min read
Nested-if statement in Python
There comes a situation in real life when we need to make some decisions and based on these decisions, we decide what we should do next. Similar situations arise in programming also where we need to make some decisions and based on these decisions we choose when to execute the next block of code. This is done with the help of a Nested if statement.
3 min read
Python Match Case Statement
Developers coming from languages like C/C++ or Java know that there is a conditional statement known as a Switch Case. This Match Case is the Switch Case of Python which was introduced in Python 3.10. Here we have to first pass a parameter and then try to check with which case the parameter is getting satisfied. If we find a match we will execute s
9 min read
with statement in Python
In Python, with statement is used in exception handling to make the code cleaner and much more readable. It simplifies the management of common resources like file streams. Observe the following code example on how the use of with statement makes code cleaner. [GFGTABS] Python # file handling # 1) without using with statement file = open('file_
8 min read
Python break statement
Python break is used to terminate the execution of the loop.  Python break statement Syntax:Loop{ Condition: break }Python break statementbreak statement in Python is used to bring the control out of the loop when some external condition is triggered. break statement is put inside the loop body (generally after if condition).  It terminates the cur
4 min read
Check multiple conditions in if statement - Python
If-else conditional statement is used in Python when a situation leads to two conditions and one of them should hold true. Syntax: if (condition): code1else: code2[on_true] if [expression] else [on_false]Note: For more information, refer to Decision Making in Python (if , if..else, Nested if, if-elif)Multiple conditions in if statement Here we'll s
4 min read
Python return statement
A return statement is used to end the execution of the function call and it "returns" the value of the expression following the return keyword to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned. A return statement is overall used to i
4 min read
How to write an empty function in Python - pass statement
During the development of a Python program, we might want to create a function without implementing its behavior. In such cases, we can define the function and use the pass statement inside it to avoid syntax errors. To write empty functions in Python, we use pass statement. pass is a special statement that does nothing. It only works as a dummy st
3 min read
Ruby redo and retry Statement
In Ruby, Redo statement is used to repeat the current iteration of the loop. redo always used inside the loop. The redo statement restarts the loop without evaluating the condition again. # Ruby program of using redo statement #!/usr/bin/ruby restart = false # Using for loop for x in 2..20 if x == 15 if restart == false # Printing values puts
2 min read
How to open a file using the with statement
The with keyword in Python is used as a context manager. As in any programming language, the usage of resources like file operations or database connections is very common. But these resources are limited in supply. Therefore, the main problem lies in making sure to release these resources after usage. If they are not released, then it will lead to
4 min read
SQLAlchemy Core - Delete Statement
In this article, we are going to see how to use the DELETE statement in SQLAlchemy against a PostgreSQL database in python. Creating table for demonstration: Import necessary functions from the SQLAlchemy package. Establish connection with the PostgreSQL database using create_engine() function as shown below, create a table called books with column
2 min read
SQLAlchemy core - Update statement
In this article, we are going to see how to use the UPDATE statement in SQLAlchemy against a PostgreSQL database in Python. Creating table for demonstration Import necessary functions from the SQLAlchemy package. Establish connection with the PostgreSQL database using create_engine() function as shown below, create a table called books with columns
3 min read
PostgreSQL UPDATE Statement
The PostgreSQL UPDATE statement is an important SQL command used to modify existing data in one or more rows of a table. It allows users to update specific columns or multiple columns at once, using conditions defined in the WHERE clause. This command is highly flexible, enabling dynamic data management and targeted updates. In this article, we wil
5 min read
Important differences between Python 2.x and Python 3.x with examples
In this article, we will see some important differences between Python 2.x and Python 3.x with the help of some examples. Differences between Python 2.x and Python 3.x Here, we will see the differences in the following libraries and modules: Division operatorprint functionUnicodexrangeError Handling_future_ modulePython Division operatorIf we are p
5 min read
Reading Python File-Like Objects from C | Python
Writing C extension code that consumes data from any Python file-like object (e.g., normal files, StringIO objects, etc.). read() method has to be repeatedly invoke to consume data on a file-like object and take steps to properly decode the resulting data. Given below is a C extension function that merely consumes all of the data on a file-like obj
3 min read
Python | Add Logging to a Python Script
In this article, we will learn how to have scripts and simple programs to write diagnostic information to log files. Code #1 : Using the logging module to add logging to a simple program import logging def main(): # Configure the logging system logging.basicConfig(filename ='app.log', level = logging.ERROR) # Variables (to make the calls that follo
2 min read
Python | Add Logging to Python Libraries
In this article, we will learn how to add a logging capability to a library, but don’t want it to interfere with programs that don’t use logging. For libraries that want to perform logging, create a dedicated logger object, and initially configure it as shown in the code below - Code #1 : C/C++ Code # abc.py import logging log = logging.getLogger(_
2 min read
Python | Index of Non-Zero elements in Python list
Sometimes, while working with python list, we can have a problem in which we need to find positions of all the integers other than 0. This can have application in day-day programming or competitive programming. Let's discuss a shorthand by which we can perform this particular task. Method : Using enumerate() + list comprehension This method can be
6 min read
MySQL-Connector-Python module in Python
MySQL is a Relational Database Management System (RDBMS) whereas the structured Query Language (SQL) is the language used for handling the RDBMS using commands i.e Creating, Inserting, Updating and Deleting the data from the databases. SQL commands are case insensitive i.e CREATE and create signify the same command. In this article, we will be disc
2 min read
Python - Read blob object in python using wand library
BLOB stands for Binary Large OBject. A blob is a data type that can store binary data. This is different than most other data types used in databases, such as integers, floating point numbers, characters, and strings, which store letters and numbers. BLOB is a large complex collection of binary data which is stored in Database. Basically BLOB is us
2 min read
twitter-text-python (ttp) module - Python
twitter-text-python is a Tweet parser and formatter for Python. Amongst many things, the tasks that can be performed by this module are : reply : The username of the handle to which the tweet is being replied to. users : All the usernames mentioned in the tweet. tags : All the hashtags mentioned in the tweet. urls : All the URLs mentioned in the tw
3 min read
Article Tags :
Practice Tags :
  翻译: