Page 249 - Ai_V1.0_Class9
P. 249

• Statements are always indented can be single or a block and are repeated till the condition is True.



                               Brainy Fact

                       When there is no change of condition in while loop then it becomes infinite loop, For example,
                                       i=1

                                  while i<6:
                                    print(i)
                       In the above code, the value of i will always be 1, and due to this, the condition will always be
                       True and will keep on executing the statement inside for an infinite number of times.



                 The different examples of while loop are:

                                                 Commands                                           Output


                     count=1                                                               hello
                     while count<=5:                                                       hello
                                                                                           hello
                         print("hello")
                                                                                           hello
                         count+=1
                                                                                           hello
                     print("Program ends")
                                                                                           Program ends


                     i = 1                                                                 1
                     while i < 6:                                                          2

                       print(i)                                                            3
                       i += 1                                                              4
                                                                                           5


                     """Input a number and check for even or odd.                          enter a number: 3
                     The whole process should continue till the user wants""" 3 is Odd

                     ans = "y"                                                             Press 'y' to continue: y
                     while ans=="y":                                                       enter a number: 6

                         num=int(input("enter a number:"))                                 6 is Even
                         if num%2==0:                                                      Press 'y' to continue: y
                            print(num," is Even")                                          enter a number: 12

                         else:                                                             12 is Even
                            print(num," is Odd")                                           Press 'y' to continue: n

                         ans=input("Press 'y' to continue:")                               Program ends here
                     print("Program ends here")






                                                                                        Introduction to Python  247
   244   245   246   247   248   249   250   251   252   253   254