91-9990449935 0120-4256464 |
While Loopwhile Loop is used to execute number of statements or body till the condition passed in while is true. Once the condition is false, the control will come out of the loop. Syntax: while <expression>: Body Here, body will execute multiple times till the expression passed is true. The Body may be a single statement or multiple statement. Eg: a=10 while a>0: print "Value of a is",a a=a-2 print "Loop is Completed" Output: >>> 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 >>> Explanation:
Program to add digits of a number: n=153 sum=0 while n>0: r=n%10 sum+=r n=n/10 print sum Output: >>> 9 >>>
Next TopicPython Applications
|