Page 138 - TechPluse_C8_Flipbook
P. 138

The while Loop Using else Statement

                  Python enables the use of else statement with the while loop. The else block is executed when
                  the condition given in the while statement becomes false.

                  Program 14: To demonstrate the use of while loop with else statement.












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

















                      JUMP STATEMENTS

                  Sometimes, there is a situation when the control of the program needs to be transferred out of
                  the loop body, even if all the values of the iterations of the loop have not been completed. For
                  this purpose, jumping statements are used in Python. Python offers two jumping statements—
                  break and continue, which are used within the loop.
                  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 15: To demonstrate the use of the break statement in a loop.
















              136       Premium Edition-VIII
   133   134   135   136   137   138   139   140   141   142   143