Continue statements

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

 

The control flow of continue statement in for loops

 

continue in for loop

 

The control flow of continue statement in while loops

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 statement 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 statement 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 statement

Program 3

This is another simple program using continue statement 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

While loop in Python

For loop in Python

Break statement in Python

Continue statement in C

 

Continue statement in C programming language
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.

Share
Published by
Karmehavannan

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

9 months ago