Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 87 - 2615_KEC
P. 87

    The while LOOP
                     The while loop repeats a block of statements while a given condition is true. In this looping construct,

                     first, the condition is checked and if it is true, the control will move inside the loop and execute the
                     statements until the condition becomes false. If the initial condition is false, then the control will
                     not enter the loop block. The syntax of while loop is as follows:
                     while(condition):

                         statement(s)
                         increment/decrement
                     Program 9: To demonstrate the use of a while loop

                         Program9.py
                      File  Edit  Format   Run    Options   Window    Help

                      #Program to demonstrate the use of a while loop

                      # Initialize a variable
                      count = 1
                      #test expression
                      while count < 5:
                          print("Count is:", count)
                      #increment/decrement
                          count += 1

                         Output

                      Count is: 1
                      Count is: 2
                      Count is: 3
                      Count is: 4

                               JUMP STATEMENTS

                     These statements are used to jump out of the loop iterations even if the condition has not become
                     false. They alter the flow of control unconditionally. The jump statements defined in Python are
                     break and continue.

                     break STATEMENT

                     The break statement is used for exiting the program control out of the loop. The break statement
                     stops the execution of the loop and program flow continues to the statement after the loop. It is
                     mostly used when we need to exit a loop prematurely before the loop condition becomes false.

                                                                                                       Flow of Control  85

| 82 83 84 85 86 87 88 89 90 91 92 |