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

10.4.3 for Statement Vs while Statement

            We have already learnt that each of the for and while statements is used for looping. We have already used a
            for  statement  to  develop  a  function  that  finds  the  factorial  of  a  number.    Now  we  ask  the  question:  can  we
            compute the factorial of a number using a while  statement? Of course, we can, as illustrated in program 10.7.
            A more serious question to ask is whether we should write a function to compute the factorial of a number using a
            while statement. The answer to this question is a resounding "NO." When all we need to do is count up or down, a
            certain number of times, a for statement should be the natural choice because that is how we think. For example,
            while moderating the results of an examination, a teacher might tell an official: Increase each student's marks by two.
            In contrast, imagine the teacher telling the official: while there are students in the class, increase a student's marks
            by two. Repeat the process of checking and updating the student's marks until there are no more students left. How
            incomprehensible is the second version? The same thing applies to computer programs. As the programs developed
            by a programmer need to be maintained. To make the programs readable, we need to be judicious in our choice of
            the control structures (or in fact, any syntactic structure), as indiscriminate use of the language's syntax makes the
            programs unreadable.

            Program 10.7 To compute factorial of a number using a while statement.

              01 def factorial(num):
              02     '''
              03     Objective: To find factorial of a number
              04     Inputs:
              05       num : the number whose factorial is to be computed
              06     Return value:
              07         product : factorial of num
              08     '''
              09
              10     product = 1
              11     count = 2
              12     while(count <= num ):
              13         product = product * count
              14         count = count + 1
              15     return product
              16 number=int(input('Enter the number for factorial '))
              17 print(factorial(number))
            Sample Output:

             >>> Enter the number for factorial 6
                 720

                    What will be the output produced when the following function is invoked with the argument 4.
                    def myFactorial(num):
                        '''
                        Objective: To find factorial of a number
                        Inputs:
                        num : the number whose factorial is to be computed
                        Return value:
                            product : factorial of num
                        '''
                        product = 1
                        count = 1
                        while(count <= num ):
                            product = product * num
                            count = count + 1
                        return product



                                                                                              Looping in Python  247
   244   245   246   247   248   249   250   251   252   253   254