Page 45 - Modular v1.1 Pyhton
P. 45

THE WHILE STATEMENT


                 The while statement executes a set of statements repeatedly,
                 until the logical expression evaluates to true. When the condition
                                                                                                                False
                 becomes false, the control comes out of the loop. The syntax of                Test Expression
                 while statement is given below.
                                                                                                       True
                 Syntax:
                 while (test expression):                                                    Execute while Block

                        Statements

                 Program 5: To demonstrate the use of a while loop.














                 On running the above program, you will get the following output:



















                  Clickipedia



                   Any non-zero value in the while loop indicates always a true condition, whereas zero indicates
                   always-false condition.

                 The Infinite Loop


                 If the condition given in a loop never becomes false, then the loop will never terminate and run
                 indefinitely. This type of loops is called an infinite loop.

                 Example:
                 while(1)
                     print(“Hello”)
                 Output: Hello will be printed infinite times.



                                                                                  Looping Statements in Python    43
   40   41   42   43   44   45   46   47   48   49   50