Page 101 - TrackpadV5.1_class8
P. 101

Program 8: To demonstrate the use of a while loop

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




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

                     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.

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

                 Program 9: To demonstrate the use of the break statement in a loop
                     Program9.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')





                                                                                        Control Structures in Python  99
   96   97   98   99   100   101   102   103   104   105   106