Python While Loop Statement

In this page you learn of python while loop statement there while Loop is used to implement number of statements or body till the state passed in while is true. Once the state is false, the control will come out of the loop.

Syntax

while <expression>:
		Body  

Example

a=10
while a>0:
    print "Value of a is",a
    a=a-2

>>> 
Value of a is 10
Value of a is 8
Value of a is 6
Value of a is 4
Value of a is 2
Loop is Completed
>>>

Program to add digits of a number:

n=153
sum=0
while n>0:
    r=n%10
    sum+=r
    n=n/10
print sum
>>> 
9
>>>