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

The above flow diagram translates into the following syntax description:

                 if   <conditional expression>:                           ……………………………..header
                      <sequence of statement(s)>                          ………………...if suite/block
                 elif <conditional expression 1>:
                       <sequence of statement(s)>                         ………………...elif suite/block 1
                 elif <conditional expression 2>:
                       <sequence of statement(s)>                         ………………...elif suite/block 2

                 elif <conditional expression 3>:
                       <sequence of statement(s)>                         ………………...elif suite/block 3
                 ….

                 elif <conditional expression n>:
                       <sequence of statement(s)>                         ………………...elif suite/block n
                 [else:
                       <sequence of statement(s)>]                        ………optional else suite/block
                 The above description can be made more concise as follows:

                 if    <conditional expression>:
                       <sequence of statement(s)>                         …………..if suite/block
                 [elif <conditional expression 1>:                        ……………………optional elif part
                       <sequence of statement(s)>…]                       ……………elif suite/block
                 [else:
                       <sequence of statement(s)>]                        ……… optional else suite/block
                 As  the  above  syntax  suggests,  several  conditional  expressions  involving  multiple  header  clauses  can  be  tested
                 one by one using an if-elif-else statement. If the conditional expression specified in the if header yields
                 (on evaluation) True, the sequence of statements in the if block is executed, and the rest of the if-elif-else

                 statement is ignored by the Python interpreter. In the other case, when the conditional expression specified in the if
                 header yields (on evaluation) False but the conditional expression specified in some of the elif headers yields
                 (on evaluation) True, then the sequence of statements in the first elif block for which its conditional expression
                 yields (on evaluation) True will be executed, and the rest of the if-elif-else statement will be ignored by the
                 Python interpreter. One can specify as many elif clauses as required in an  if-elif-else statement. When the
                 conditional expression specified in the if header, as well as each of the elif headers, yields (on evaluation) False,
                 the sequence of the statements in the else block will be executed. However, it is not mandatory to include an else
                 clause. All the statements in a block are indented at the same level. As mentioned above, as soon as the conditional
                 expression in a header yields True, the sequence of statements in its block is executed, and control moves to the
                 statement immediately following the if-elif-else/ if-elif statement.
                 Next, let us rewrite Program 10.5, making use of the elif clause.

                 Program 10.6 Write a function grade(percentage) that computes the student's grade based on the student's
                 percentage and the cut-off marks specified for that grade using elif clause.

                                                         percentage         grade
                                                            >=75              A
                                                            >=60              B
                                                            >=50              C
                                                            >=33              D
                                                            < 33              F


                                                                                               Conditional Statements  263
   272   273   274   275   276   277   278   279   280   281   282