Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 83 - 2615_KEC
P. 83

    else:
                                 statement(s)

                     Program 3: To find the greatest number between two numbers.
                         Program3.py

                      File  Edit  Format   Run    Options   Window    Help

                      #Program to compare two numbers using if else statement
                      a = int(input('Enter the first number: '))
                      b = int(input('Enter the second number: '))
                      if(a > b):
                          print('First number is greater than second number')
                      else:
                          print('Second number is greater than first number')

                         Output

                      Enter the first number: 12
                      Enter the second number: 25
                      Second number is greater than first number

                     THE if-elif-else STATEMENT
                     Sometimes, we need to evaluate multiple statements to get a certain result. In such scenarios, we

                     can use the if-elif-else statements to evaluate          Start
                     multiple scenarios. First, it checks and
                     evaluates the first condition. If it is true, it will
                     execute the respective statement(s). However,         if condition 1  True
                     if the condition is false, it moves to the elif                              Statement 1
                     statement and evaluates those conditions.                   False
                     Finally, if none of the conditions evaluates to
                     true, it executes the else block. The syntax of      if condition 2  True    Statement 2
                     if-elif-else statement is as follows:

                         if (conditional expression):                            False
                                 statement(s)
                         elif (conditional expression):                                   True
                                 statement(s)                             if condition 3         Statement 3
                         elif (conditional expression):                          False
                                 statement(s)                                                     Statement
                         else:
                                 statement(s)                                                                   Stop

                                                                                                       Flow of Control  81

| 78 79 80 81 82 83 84 85 86 87 88 |