Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 83 - 2612_Digimind
P. 83

    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

                         PURE
                          FACT       A loop can run 0 times if the condition is false at the beginning.

                     Program 4: To demonstrate the use of a while loop

                         Program4.py
                      File  Edit  Format   Run    Options   Window    Help

                      #Program to demonstrate the use of a while loop

                      # Initialise 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

                          Tick ( ) if you know this.

                          ▶  Iterative statements are also known as looping statements.
                          ▶  A sequence is a succession of values bound together by a single name.

                                                                                                    Iteration in Python  81

| 78 79 80 81 82 83 84 85 86 87 88 |