Page 109 - modular4.0
P. 109

Chapter Profile
                 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 an infinite number of times.

                 To come out of the infinite loop, close the interactive mode and restart the idle.


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







                        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, jump statements are used in Python. Python offers two jump statements—break and
                 continue, which are used within the loop.


                 The break Statement

                 The break is a keyword in Python which is used for sending 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:

                 #loop statement
                     break


                                                                                                     Loops in Python  107
   104   105   106   107   108   109   110   111   112   113   114