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

False       Test
                                                              condition



                                                                  True

                                                             Enter Loop




                                                              Continue

                                             Exit Loop
                                                Fig 10.5: Working of continue statement

            As the control moves to the next iteration, firstly, the loop variable is updated with the next value in the sequence
            or range. Thereafter, if the updated loop variable is within the range/ sequence or if the test condition is True, the
            execution of the statements in the body of the loop takes place. Consider the examples given below, that explain the
            working of continue statement in for loop and while loop.


                         for x in range(1, 5):                   num = 5
                             if x == 3:                          while num <= 10:
                                 continue                            if num % 4 == 0:
                             else:                                       num = num + 1
                                 print(x)                                continue
                         print('Over')                               else:
                                                                         print(num * num)
                                                                         num = num + 1
                                                                 print("Done")

                         Output:                                 Output:
                         1                                       25
                         2                                       36
                         4                                       49
                         Over                                    81
                                                                 100
                                                                 Done



                   break and continue statements are usually part of the body of the if statement.



            Program 10.9 To accept five numbers (except 0) and display their product.

              01 #Objective: To accept five numbers (except 0) and display their product.
              02 P = 1
              03 for count in range(1, 6):
              04     num=int(input('Enter a number :  '))
              05     if num == 0:
              06      print(' ZERO will not be multiplied')


                                                                                              Looping in Python  251
   248   249   250   251   252   253   254   255   256   257   258