In programming there will be some statements or group of statements which we want to execute continuously for more than one time, this is where loops comes in the picture. Looping allow us to continuously execute particular statement or block of statement to more than one time. Looping will check for the condition, and will continuously execute particular statement or block of statement until the condition get false or invalid. Repeated execution of a set of statements is called as iteration. This statements are executed in order except when a jump statements are encountered.
In python while loop is used to iterate over a sequence like list,string, tuple etc and other iterable objects. Iterating over a sequence is called as traversal. We can also embed conditional statements in for loop. Syntax Here is the simple syntax for looping statement in python : while loop−
while expression:
statement(s)
In this statements can be single or multiple i.e block of statement with uniform indent.
The condition or expression may be Logical expression.
The condition will return True for non – zero value and the loop will iterate continuously till the condition remains True.
If the conditions turns to be False then the control will be shifted directly to the set of lines present immediately after the loop.
As python uses indentation to group statements just as curly braces in other programming languages, all statements in code black will be indented uniformly to ensure that they are part of same code block.
<strong>while loop in python (itvoyagers.in)</strong>
Explanation If the logical expression turns out to be False than the entire set of code assigned inside that block will be skipped. And if the condition is True than the loop is executed and corresponding set of code also gets executed. We can say that the while loop might not run even once in case of False logical expression.
Example of while loop in python
""" Author : ITVoyagers (itvoyagers.in)
Date :17th August 2020
Description : Program to show use of while loop in python """
counter=0
while counter < 10:
print("count is",counter)
counter+=1
Output
<strong>Output of while loop in python</strong>
Single Statement Suites
Just as we write if statement , we can also write while statement with its executable statement in while header if its executable block consist of single line.
Example of one line while loop in python
""" Author : ITVoyagers (itvoyagers.in)
Date :17th August 2020
Description : Program to show use of one line while loop in python """
num=10
while num < 20: num = num + 1; print("num is",num)
Output
<strong>Output of one line while loop in python</strong>
while-else loop
Python allows us to use else statement with loop statement.
In while loop we can use else statement.
In while loop if an else statement is used then the else block will be executed only if the expression or condition in while statements returns False.
Example of while else loop statement
""" Author : ITVoyagers (itvoyagers.in)
Date :17th August 2020
Description : Program to show use of while else loop """
print("Output of while else statement")
num=0
while num < 10:
print(num, "is less than 10")
num+=1
else:
print(num, "is not less than 10")
Output
<strong>Output of while else loop </strong>
More examples of while loop
""" Author : ITVoyagers (itvoyagers.in)
Date :17th August 2020
Description : Program to show use of while loop """
num=1
while num < 10:
num = num + 1
print("num is",num**3)
Output
Output
""" Author : ITVoyagers (itvoyagers.in)
Date :17th August 2020
Description : Program to show use of while else loop """
num=8
while num < 10:
num = num + 1
print("num is",num**3)
else:
print("condition not satisfied Loop finished")