Change language

Python Continue Statement

| | |

Using loops in Python automates and repeats tasks efficiently. But sometimes a condition may arise where you want to exit the loop completely, skip iteration, or ignore this condition. This can be done using loop control statements. Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects created in that scope are destroyed. Python supports the following control statements.

  • Continue statement
  • Break statement
  • Go through statement

In this article the focus will be on the continue statement.

Continue statement

Continue is also a loop control statement, just like the break statement. The continue statement is the opposite of the break statement, instead of terminating the loop, it forces the next iteration of the loop to run. 
As the name suggests, the continue statement causes the loop to continue or to perform the next iteration. When the continue statement is executed in a loop, the code within the loop following the continue statement will be skipped and the next iteration of the loop will begin.

Syntax :

 continue 

Example:
Consider the situation when you need to write a program that prints a number between 1 and 10, not 6. It is specified that you should do this with a loop, and only one loop is allowed. 
This is where the continue statement is used. Here we can start a loop from 1 to 10, and every time we need to compare the value of the iterator with 6. If it is 6, we will use the continue statement to go to the next iteration, without printing anything else, we will print the value.

Below is the implementation of the above idea:

# Python program for
# demonstrate continued
# statement

 
# loop from 1 to 10

for i in range ( 1 , 11 ):

  

# If I am 6

# go to next iteration

# no printing

if i = = 6 :

continue

else :

# otherwise output the value

# I

print (i, end = " " )

Exit:

 1 2 3 4 5 7 8 9 10 

The continue statement can be used with any other loop, for example, at the same time as for the for loop described above.

Exercise Problem:
Given the number n, print a triangular pattern. We are only allowed to use one loop.

 Input: 7 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * 

Solution:

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically