Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 86 - 2611_SmartGPT Pro V(5.0) C-8
P. 86

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

                      THE BREAK STATEMENT

                      The break statement is used for exiting the program          A loop repeatedly executes a block
                      control out of the loop. The break statement stops            of code, just as a flock of Starlings
                      the execution of the loop and program flow continues           (birds) repeatedly adjusts their
                      to  the  statement  after  the  loop.  A  single  break                   formation.
                      statement will break out of only one loop.

                      Syntax:
                          break
                      Program 8: To print first 12 whole numbers. Stop printing if number 7 is encountered.

                          Program8.py
                       File  Edit  Format   Run   Options  Window    Help

                       #Program to show the use of the break statement in the for loop
                       for number in range(0, 12):
                           if(number == 7):
                               break
                           print('Number is:', number)
                       print('Out of loop')

                      On executing 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

                       84   Computer Science (V5.0)-VIII

| 81 82 83 84 85 86 87 88 89 90 91 |