Page 251 - Computer Science Class 11 With Functions
P. 251

10.5 Jump Statements

            We have learnt that loops are used to execute the statements repeatedly in the same sequence as they are given in
            the body of the loop. However, sometimes, we may require to either exit the loop or skip certain statements of the
            loop before moving to the next iteration. Jump statements help to control the loop in such a manner. The two jump
            statements provided by Python are break and continue.
            10.5.1 break Statement

            The break statement terminates the same loop in which it is defined and moves the control to the next statement
            immediately following the loop. Once the break statement is encountered and executed, no further statement in
            the loop will be executed.
            Syntax:
              break
            The flowchart in fig 10.4 explains the working of break statement.





                                                          Test      False
                                                        condition


                                                             True

                                                        Enter Loop



                                                          Break



                                                        Exit Loop
                                       Fig 10.4: Flowchart to explain the working of break statement
            The break statement causes unconditional termination of a loop.  Even if the test condition is True, or all values
            in the range have not been traversed, yet, as the break statement is encountered, the execution of the loop stops.
            Consider the code explaining the functioning of break statement in a for loop and while loop:


                         for x in range(1,5):                    num=5
                             print(x)                            while num>=0:
                             if x==3:                                print(num*num)
                               break                                 if num%3==0:
                         print("Over")                                 break
                                                                     num=num-1
                                                                 print("Done")

                         Output:                                 Output:
                         1                                       25
                         2                                       16
                         3                                       9
                         Over                                    Done
            In the example of for loop, as the value of x becomes equal to 3, the test condition of if statement evaluates
            to True, which leads to the execution of break statement.



                                                                                              Looping in Python  249
   246   247   248   249   250   251   252   253   254   255   256