Page 122 - TP_Play_V2.1_class8
P. 122

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





                  120       Play (Ver. 2.1)-VIII
   117   118   119   120   121   122   123   124   125   126   127