Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 91 - 2629_Devagiri_C-8
P. 91

    Let us look at an example:

                      Program 5 To print the first five natural numbers.

                         Program5.py                                                                Output
                      File  Edit  Format   Run    Options   Window    Help                       1
                                                                                                 2
                      i=1
                                                                                                 3
                      while i<=5:
                                                                                                 4
                          print(i)
                                                                                                 5
                          i+=1

                            THE INFINITE LOOP

                     An infinite loop is a loop that never ends because its condition is always true and never becomes
                     false. For example, if a loop is supposed to stop when a certain variable reaches a value but that
                     variable is never changed inside the loop, the condition remains true forever.

                      Program 6 To demonstrate the execution of an infinite loop.

                         Program6.py                                                             Output
                      File  Edit  Format   Run    Options   Window    Help                    Orange Education

                                                                                              Orange Education
                      i=1
                                                                                              Orange Education
                      while(i<10):
                                                                                              Orange Education
                          print("Orange Education")
                                                                                              Orange Education

                     Here, the value of i is not changed and it will always be less than 10, so Orange Education will keep
                     on printing infinitely.
                     To exit the  infinite  loop, you  can close  the  program window. This will  break  the  execution  of

                     the program.

                      RAPID RECALL                            Tick ( ) if you know this.

                         1.  Iteration means repeating a set of instructions again and again.

                         2.  The for loop repeats a block of statements a fixed number of times.

                                                                                                                      89
                                                                                                   Networks Around Us

| 86 87 88 89 90 91 92 93 94 95 96 |