Page 119 - 2622_Delhi Police Public School_C-8
P. 119

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





                 The continue Statement

                 The  continue  statement  is  used  inside  loops.  When  a
                 continue statement is encountered inside a loop, control         A loop repeatedly executes a block of
                 of the program jumps to the beginning of the loop for the        code, just as a flock of Starlings (birds)
                 next iteration, skipping the execution of the rest of the         repeatedly adjusts their formation.
                 statements inside the loop for the current iteration.

                 Syntax:
                      continue



                          Interdisciplinary Learning                                                    Lab Activity


                    Write a program in Python to find whether the number is prime or not.











                                                                                                 Loops in Python   117
   114   115   116   117   118   119   120   121   122   123   124