Page 141 - Touchpad_Plus_V3.2_Class 8
P. 141

Program 7: To demonstrate the use of the break statement in a loop.

                                                             Program7.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')



                 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 of the program jumps to the beginning of the loop for next iteration, skipping the
                 execution of rest of the statements of the loop for the current iteration.
                 The syntax of the continue statement:

                 #loop statements
                      continue
                 #the code to be skipped

                  Program 8: To demonstrate the use of the continue statement in a loop.

                                                             Program8.py
                   File  Edit  Format  Run    Options   Window    Help


                  #Program to show the use of the continue statement
                  for number in range(0, 12):
                      if(number == 8):
                          continue
                      print('Number is:', number)
                  print('Out of loop')






                                                                                               Loops in Python       139
   136   137   138   139   140   141   142   143   144   145   146