Page 139 - CodePilot V5.0 C7
P. 139

Output

                  Enter a number:9
                  You have entered a positive number.
                  Odd

                  Enter a number:-4
                  You have entered a negative number.
                  Even




                 IF..ELIF….ELSE STATEMENT

                 Python provides the elif(else-if) statement
                 to check for multiple conditions and execute             Start
                 the code block within if any of the conditions
                 are evaluated to be true.                                            True
                 The elif statement is optional like the else          if condition 1           block 1
                 statement  but multiple  elif statements
                                                                             False
                 can be used in a block of code following an if
                 statement.                                                           True

                 Syntax of the if..elif….else statement:              if condition 2            block 2

                 if condition1:
                                                                             False
                     Statement block 1

                 elif condition2:                                                     True
                                                                      if condition 3            block 3
                     Statement block 2
                 elif condition3:                                            False
                                                                                                 block
                     Statement block 3
                     ..                                                                                     Stop

                     ..
                 else:

                     Statement block n
                 The interpreter starts at the top, testing condition1. If it is true, statement block 1 is executed and
                 the rest of the constructs are ignored. If it is false, the control moves down and checks condition2,
                 if it is true, statement block 2 will be executed and if it is false, it moves next to condition3 and so
                 on. The process repeats till a true condition is found. In case none of the conditions evaluate to
                 true, the block after the else is executed.

                  Program 6 To take marks of a student as input and display a message based on the marks:
                                ∑  If the marks are equal or more than 90, show “Excellent”
                                ∑  If equal or more than 70, show “Very Good”





                                                                                                                  137
                                                                                           Flow of Control in Python
   134   135   136   137   138   139   140   141   142   143   144