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
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.
The syntax of the continue statement
continue
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
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
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 keyword in Java Language
Continue statements in C language
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…