Categories: Python

Continue statement in Python language

Continue statement in Python language

In this tutorial. we will discuss the Continue statement in Python language

In this post, we will learn about the usage of “continue” in “for loop” and “while loops” in Python. Continue is a keyword in python that is used to control the flow of loops. This statement can be used inside for and while loops. The continue statement provides with an option to skip the current flow of the loop until the exit from the loop

Syntax

Syntax of continue

Flow diagram for continue


Control of the flow of loops of “continue” in “for loop”
Control the flow of loops of “continue” in “while loop”

Continue statement for loop in Python


In these examples, we will explain how to control the flow by “continue” statement in “for loop” in Python language.

This program allows to display a range of number from 1 to 10  using “for loop” but value 6 is skipped by continue statement.

Program 1

In this program, value num is initialized at 1 (range 1 to 10). Then, “for loop” creates the loop as long as the variable number is between 1 to 10.

In this “for loop“, the number increases by 1 with each step.
Here, we see that the number 6 never occurs as output because number 6 is skipped by continue statement.
But the “for loop” continues after skipping the value 6, then the lines start for numbers 7-10, before leaving the loop.

When the above code is executed, The following output is displayed

1 2 3 4 5 7 8 9 (equivalent value 6 skipped)

Program 2

This program allows the display of number from 1 to 10 from a list of numbers in python. But value 5 is skipped by the continue statement.

This program uses the “continue” statement in python that contains a list of named number with  values from 1 to 10. The “for loop” constructs the print element of number list.
Then, there is an “if statement” that presents a condition with “continue statement”. In the “if condition“, the variable number is equivalent to integer value of 5.
Then the loop will continue having skipped the value  5.

When the above code is executed, The following output is displayed

1 2 3 4 6 7 8 9 10

//continue statement skipped value of 5



Continue statement in while loop of Python


Program 1

When the above code is executed, The following output is displayed
1
2
3
4
5

Same program but continues with while loop. We can see, the “continue” skipped the value 3 in this program.



Program 2

This program allows to display  from 1 to 10 using “while loop” in python. But value 5 is skipped by continue

When the above code is executed, The following output is displayed

(‘printed values’, 1)
(‘printed values’, 2)
(‘printed values’, 3)
(‘printed values’, 4)
(5, ‘value skipped’)
(‘printed values’, 6)
(‘printed values’, 7)
(‘printed values’, 8)
(‘printed values’, 9)

(‘printed values’, 10)

At the above program, int I initializes 1, while loop test- statement checks the conditionuntil the condition is true. So the expected display is between 1 to 10 integer number. But at the “if condition“, “continue” skips the value 5.

Similar post

for loop with pass, break continue statements

while loop with pass break continue statements

Continue in C Language

break in C Language

Break statement in Python

Pass statement in Python

Karmehavannan

Share
Published by
Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.