Table of Contents
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
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.
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
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’, 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