Page 147 - TP_Plus_v2.2_Class_8
P. 147

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


                     Program6.py
                  File  Edit  Format   Run   Options   Window     Help


                  #Program to demonstrate the use of while loop with else statement
                  i = 1
                  while (i <= 5):
                      print(i)
                      i += 1
                  else:
                      print('The While loop ends here!')



                 You will get the following output:


                     Output

                  1
                  2
                  3
                  4
                  5
                  The While loop ends here!




                      Infinite Loop

                 If the condition given in a loop never becomes false, then                 Tech Fact
                 the loop will never terminate and run indefinitely. This           Any non-zero value in the
                 type of loop is called an infinite loop.
                                                                                   while loop indicates always a
                 Example:                                                          true condition, whereas zero

                 while(1)                                                        indicates always-false condition.
                     print(“Hello”)
                 Output: Hello will be printed infinite times.
                 To come out of the infinite loop, we can either close the program window or press Ctrl + C. This will
                 break the running of the program.



                        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.



                                                                                                  Loops in Python  145
   142   143   144   145   146   147   148   149   150   151   152