Page 140 - Plus_V2.2_C8_Flipbook
P. 140

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.
                  To come out of the infinite loop, we can either close the program window or press Ctrl + C. This will
                  break the running of the program. In fact, at any time during the execution of a program in Python,
                  you can press the key combination Ctrl + C to stop running the program.


                       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.














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





















                         JUMP STATEMENTS


                  Sometimes, there is a situation when the control of the program needs to be transferred out of the
                  loop body, even if all the values of the iterations of the loop have not been completed. For this purpose,
                  jumping statements are used in Python. Python offers two jumping statements—break and continue,
                  which are used within the loop.



                  138   Premium Edition-VIII
   135   136   137   138   139   140   141   142   143   144   145