Page 250 - Computer Science Class 11 With Functions
P. 250

10.4.4 More Examples of while Statement

        In Table 10.3, we give some more examples of the while loop.
                                             Table 10.3: Examples of while loop

                        Statements                       Output                      Explanation
         i = 1                                      123456            As  the  value  of  i becomes  7,  the  condition
         while i <= 6:                                                evaluates to False and the loop terminates.
            print(i, end='')                                          The  int  objects  are  displayed  one  after  the
            i += 1                                                    other without any spaces as end=''.
         i = 1                                      sum: 55           As  the  value  of  i becomes  11, the loop
         count = 0                                                    terminates  and the  statement  in the  else
         while i<=10:                                                 block is executed.
            count=count+i
            i =i +1
         else:
            print('sum:', count)
         n = 8                                      64 36 16 4        As  long  as  the  test  condition,  n>0 is  True,
         while n > 0:                               Done              the statements in the body of the while loop
             print(n*n, end=' ')                                      will  be  executed.  After  the  loop  terminates
             n = n-2                                                  normally  (without  a  break  statement),  the
                                                                      statement  in  the  else  block  print("\nDone")
         else:
                                                                      gets executed.
             print('\nDone')
        In the case of a while loop, the test condition is checked at the entry point of the loop. So, the loop may not be
        executed even once, if the test condition is False. Therefore, always remember to initialize the control variable
        before the loop begins. If the initial value is not given to the control variable, the while  loop will not execute.
        Secondly, the control variable has to be updated inside the while loop, otherwise, the loop will never terminate and
        become an infinite loop.


                 1.  Consider the code given below:
                    for num in range(10,30,3):
                        print(num-1)
                    Identify the following components of the given loop:

                    a. Control variable
                    b. Initial value of control variable
                    c. Final value of control variable
                    d. Step Value
                    e. Body of loop
                 2.  Is the following code elegant? If not, rewrite it to make it more elegant:
                    num = 10

                    while(num<30):
                        print(num-1, end = ' ')
                        num = num+3



         248   Touchpad Computer Science-XI
   245   246   247   248   249   250   251   252   253   254   255