Continue statement in Python programming language

Continue statement in Python programming language

In this tutorial, we will get to know the uses of Continue statement in Python programming language

The tutorial briefly explains  the “continue statement” of  for loop and while loop in python language

Description

Continue is a keyword in Python language which controls the flow of the loop. This statement can be used inside of both the for loop and while loop. The continue statement provides us with the option to skip the flow of control(stated under if )of the loop until exit from the loop.

Declaration

The syntax of the continue statement

continue

Flow diagram for continue in python

Continue statement in Python programming language
Continue statement in Python

 

The control flow of continue statement in for loops

 

Continue statement in Python programming language
continue in for loop

 

The control flow of continue statement in while loops

Continue statement in Python programming language
continue in wile loop

 

Example for continue statement in python

The following example shows how the continue statement works with for loop in python language

This program display range of numbers from 1 to 10 using for loop

Python continue statement in for loop

program 1

'''for loop in Python'''
#create a list of numbers- Python list
numbers=[1,2,3,4,5,6,7,8,9,10]
for num in numbers: #Python for loop
    print num,

 

When the above code is compiled and executed, it will produce the following results

1 2 3 4 5 6 7 8 9 10

This is a program using continue  in Python which contains a list of numbers with the assigned value from 1 to 10. The for loop constructs to display elements of numbers in the list.

 

program 2

'''for loop in Python'''
#create a list of numbers- Python list
numbers=[1,2,3,4,5,6,7,8,9,10]
for num in numbers: #Python for loop
    if num==5:
        continue
    print num,

 

These same program displays a range of numbers from 1 to 10 using for loop where the value 5 is skipped by the continue statement

Then, when there is an “if statements” with both the condition and continue  in the if condition, variable number is equivalent to 5.

Then, the loop will continue with the skipped  value of 5 as shown below

 

When the above code is compiled and executed, it will produce the following results

1 2 3 4 6 7 8 9 10

//value 5 skipped by continue statement

 

Python continue statement in while loop

Program 1

'''while loop in Python'''
i=1;
while(i<=10):
        print i,
        i=i+1;

 

When the above code is compiled and executed, it will produce the following results

1 2 3 4 5 6 7 8 9 10

 

Program 2

'''continue statements in Python'''
i=1;
while(i<=10):
    if(i==5):
        i=i+1;
        continue
    print i,
    i=i+1;

 

When the above code is compiled and executed, it will produce the following results

1 2 3 4  6 7 8 9 10

This program is python while loop  but 5 is skipped by continue

Program 3

This is another simple program using continue  in while loop in python language

'''continue statements in Python'''
i=100;
while(i>=10):
    if(i==30):
        print(i,"value skipped")
        i=i-10;
        continue
    print ("printd value",i)
    i=i-10;

 

When the above code is compiled and executed, it will produce the following results

('printed value', 100)
('printed value', 90)
('printed value', 80)
('printed value', 70)
('printed value',60)
('printed value', 50)
('printed value', 40)
(30, 'value skipped')
('printed value', 20)
('printed value', 10)

 

Suggested for you

Break statement in Python

break keyword in  Java Language

Keyword in Python language

Continue statements in Python

Continue statements in C language

Keyword in C language

Keyword in Java language

for loop in Python

While loop in Python

 

 

By Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Leave a comment

Your email address will not be published. Required fields are marked *

5Shares