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

In the above code snippet, the test expression always yields True. But you may ask, why would anybody write a
        code like the one above. Of course, no one would, except by an error. Next, consider the following code snippet.
        The code aims to find the sum of positive numbers entered by a user. The control leaves the loop as soon as it encounters
        zero or a negative number, and the sum of the positive numbers is printed.
        sumNums = 0
        num = int(input('enter a number: '))
        while num>0:
            sumNums = sumNums + num
            num =  int(input('enter a number: '))
        print('Sum of all numbers: ', sumNums)
        In the above code, we are required to include the code to input a number twice, whereas it is indeed part of the loop.
        So, the following code that sets the Boolean expression in the while loop as True would be better:

        sumNums = 0
        while True:
            num = int(input('enter a number: '))
            if num>0:
                sumNums = sumNums + num
            else:
                break
        print('Sum of all numbers: ', sumNums)
        10.9 pass Statement

        A pass statement is ignored by the Python interpreter. It has the following simple syntax:

        pass
        It is often used as a stub when we want to leave some functionality to be defined in the future. For example, let us
        assume you don't want to say how much a test result should be changed for moderation.  Instead, you want it to be
        determined statistically from the result of the examination. However, you may not yet have thought about how to do
        it. So, you may define a function that just contains a pass statement. For example,

        def moderate(result):
            pass

         Program 10.19 Result moderation : left for future
        Also, pass statement can also be used when no action is required on some conditions. For example,

        for letter in 'world':
            if letter == 'l':
                pass
            else:
                print(letter, end='')
        Sample Output:
        word
        It is important to remember  that only the pass statement is ignored, other statements if any in the block are not
        ignored. For example,

        for letter in 'world':
            if letter == 'l':
                pass
                print(letter, end ='')
            else:
                print(letter, end='')
        Sample Output:

        world

         260   Touchpad Computer Science-XI
   257   258   259   260   261   262   263   264   265   266   267