Page 121 - TP_Play_V2.1_class8
P. 121

You will get the following output:

                     Output

                  1
                  2
                  3
                  4
                  5
                  The While loop ends here!





                    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.

                 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 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.

                 The break Statement


                 The break is a keyword in Python which is used for bringing the program control out of the loop.
                 The break statement halts the execution of a loop and program flow switches to the statement
                 after the loop. A single break statement will break out of only one loop. The syntax of the break
                 statement is as follows:

                                                                             Just as the heart repeatedly
                 #loop statement
                                                                              pumps blood, iteration in
                     break                                                programming involves repeatedly
                                                                             running a set of instructions.







                                                                                            Loops in Python      119
   116   117   118   119   120   121   122   123   124   125   126