Page 141 - Plus_V2.2_C8_Flipbook
P. 141

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.















                 On running the above program, we get the following output:
























                      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




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