Page 104 - TP_Play_V2.2_Class8
P. 104

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

                  File  Edit  Format   Run   Options   Window    Help

                  #Program to demonstrate the use of a while loop

                  # Initialize a variable
                  count = 1
                                                                                         Output
                  #test expression
                  while count < 5:                                                    Count is: 1
                      print("Count is:", count)                                       Count is: 2
                      count += 1                                                      Count is: 3
                  #increment/decrement                                                Count is: 4





                                                                  Hintbot


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



              The While Loop Using Else Statement

              Python enables the use of else statement with the while loop. The else block is executed when the
              condition given in the while statement becomes false.

              Program 6: To demonstrate the use of while loop with else statement
                   Program6.py
               File  Edit  Format    Run   Options   Window    Help


                #Program to demonstrate the use of while loop with else statement
                i = 1
                while (i <= 5):
                    print(i)
                    i += 1
                else:
                    print('The While loop ends here!')


              On running the above program, we get the following output:

                   Output

                1
                2
                3
                4
                5
                The While loop ends here!





                  102  Plus (Ver. 4.0)-VIII
   99   100   101   102   103   104   105   106   107   108   109