Page 218 - Computer Science Class 11 Without Functions
P. 218

C T  03     1.  Consider the code given below:

                         for num in range(10,30,3):
                             print(num-1)
                         Identify the following components of the given loop:
                         a.  Control variable
                         b.  Initial value of control variable
                         c.   Final value of control variable
                         d.  Step value
                         e.  Body of loop
                      2.  Is the following code elegant? If not, rewrite it to make it more elegant:
                         num = 10
                         while(num<30):
                             print(num-1, end = ' ')
                             num = num+3


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

        9.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 9.5: 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.

         216   Touchpad Computer Science-XI
   213   214   215   216   217   218   219   220   221   222   223