Page 133 - ComputerGenius_V2.1_Class8
P. 133

Output:
                   1
                   2

                   3
                   4
                   5

                 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 loop is called an infinite loop.
                 Example:
                 while(1):
                     print("Hello")
                 Output: Hello will be printed infinite times.

                 To come out of the infinite loop, we can either close the program window. This will break the running
                 of the program.

                                                                Notes

                              We should not  use assignment operator in place of equality comparison
                                   operator in the test condition as it may cause an infinite loop.


                 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.

                 Example 9: To demonstrate the use of while loop with else statement.
                 Program:
                   i=1

                   while i<5):
                       print (i)
                       i=i+1
                   else:
                      print ('The while loop ends here')
                 Output:

                   1
                   2
                   3

                   4

                   5
                   The while loop ends here

                                                                                    Control Flow Statements   131
   128   129   130   131   132   133   134   135   136   137   138