Page 50 - tp_Modula_v2.0
P. 50

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.

                  Syntax:

                  #loop statement
                      break
                  Program 7: To demonstrate the use of the break statement in a loop.
                      Program7.py

                   File  Edit  Format   Run   Options  Window    Help

                   for number in range (0, 12):
                       if number == 7:
                           break
                       print('Number is:', number)
                   print('Out of loop')




                  On running the above program, you will get the following output:


                      Output
                   Number is: 0
                   Number is: 1
                   Number is: 2
                   Number is: 3
                   Number is: 4
                   Number is: 5
                   Number is: 6
                   Out of loop





                  The continue Statement

                  The continue statement is used inside loops. When a continue statement is encountered inside
                  a loop, control of the program jumps to the beginning of the loop for next iteration, skipping the

                  execution of rest of the statements of the loop for the current iteration.


                   48     Touchpad MODULAR (Version 2.0)
   45   46   47   48   49   50   51   52   53   54   55