Page 148 - TP_Plus_v2.2_Class_8
P. 148

The Break Statement

                  The break is a keyword in Python which is used for bringing the program control out of the loop. The

                  break statement halts the execution of a loop and program flow switches to the statement after the loop.
                  A single break statement will break out of only one loop. The syntax of the break statement is shown
                  below.

                  Syntax:
                  #loop statement

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

                  Syntax:
                  #loop statements
                       continue

                  #the code to be skipped
                  146   Plus (Ver. 2.2)-VIII
   143   144   145   146   147   148   149   150   151   152   153