Page 117 - 2622_Delhi Police Public School_C-8
P. 117

The syntax of while statement is given below:

                 while (test expression):                                                           Hintbot
                        Statements
                                                                                       To generate a series in reverse
                        increment/decrement expression                                  order: "start" value must be
                                                                                        greater than "stop" value and
                                                                                       "step size" should be negative.

                 Program 6: To print first 5 natural numbers using while loop.

                     Program6.py                                                 Output

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



                 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                                                 Output

                  File  Edit  Format   Run    Options  Window     Help        1
                                                                              2
                  i=1
                                                                              3
                  while i<= 5:
                                                                              4
                      print(i)
                                                                              5
                      i=i+1
                                                                              The while loop ends here
                  else:
                      print('The while loop ends here')







                                                                                                 Loops in Python   115
   112   113   114   115   116   117   118   119   120   121   122