Page 276 - Computer Science V2.0 Class 11
P. 276

30 percentage = int(input("Enter student's percentage: "))
                31 print('Your grade is', grade(percentage))
              Sample Output:

               >>> Enter student's percentage: 75
                    Your grade is A
              10.3.3 if-elif-else Statement/ if-elif Statement

              Both the if and if-else statements check for one conditional expression and, based on whether the conditional
              expression yields (on evaluation) True or False, execute the if suite (also called the if block) or the else
              suite (also called the else  block) respectively. But sometimes, there arises a need to check multiple conditional
              expressions. For example, in program 10.6, different actions were required to be executed depending on the value of
              a variable percentage. In such situations, we use the elif clause. Fig 10.5 depicts the control flow in such situations.






                                              Test             True        Sequence of Statements
                                            Condition                           (if block)




                                                  False





                                              Test             True        Sequence of Statements
                                           Condition 1                        (elif block 1)




                                                  False




                                              Test             True        Sequence of Statements
                                           Condition 2                        (elif block 2)





                                                  False





                                              Test             True        Sequence of Statements
                                           Condition n                        (elif block n)




                                                  False

                                     Sequence of Statements
                                          (else block)

                                       Fig 10.5: Block diagram (Flowchart) of if-elif-else statement

               262   Touchpad Computer Science (Ver. 2.0)-XI
   271   272   273   274   275   276   277   278   279   280   281