Page 75 - TP_V5.1_C8_fb
P. 75

Tick ( ) if you know this.

                      ▶  Iterative statements are also known as looping statements.
                      ▶  The while loop is used to execute a block of statements repeatedly for a given number of times
                        until the specified condition becomes false.




                           JUMP STATEMENTS


                 These statements are used to exit the loop or current iteration even if the condition has not become
                 false. They alter the flow of control unconditionally. The jump statements defined in Python are
                 break and continue.


                 THE break STATEMENT
                 The break statement is used for exiting the program control out of the loop. The break statement
                 stops the execution of the loop and program flow continues to the statement after the loop. A

                 single break statement will break out of only one loop.
                 Program 5: To demonstrate the use of the break statement in a loop.

                     Program5.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 running the above program, we 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 for loop will print the numbers from 0 to 6 because as soon as the number reaches 7, the
                 break statement is executed, stopping the loop.


                                                                                               Iterations in Python  73
   70   71   72   73   74   75   76   77   78   79   80