Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 85 - 2611_SmartGPT Pro V(5.0) C-8
P. 85

    THE INFINITE LOOP
                     If the condition given in a loop never becomes false, then the loop will never terminate and run

                     indefinitely. This type of loop is called an infinite loop.
                     Example:

                     while(1):
                         print(“Hello”)
                     Output: Hello will be printed infinite times.

                     To come out of the infinite loop, we can either close the program window. This will break the
                     running of the program.

                     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 7: To demonstrate the use of while loop with else statement.

                         Program7.py
                      File  Edit  Format  Run   Options   Window    Help

                      i=1
                      while i<= 5:
                          print(i)
                          i=i+1
                      else:
                          print('The while loop ends here')

                         Output
                      1

                      2
                      3
                      4
                      5
                      The while loop ends here

                                              1.  Write the syntax of for loop.
                             uiz   Bee           _____________________________________________________________

                                              2.  Write the syntax of while loop.
                                                 _____________________________________________________________

                                                                                                      Loops in Python  83

| 80 81 82 83 84 85 86 87 88 89 90 |