Page 102 - KEC Khaitan C8.5 Flipbook
P. 102

THE if-else STATEMENT
                                                                                     Start
                  The if-else statement is used to evaluate whether a
                  given statement is true or false. If the given condition is

                  true, then statements within the if block are executed.          if condition
                  If the given condition is false, then statements within
                  the else block are executed. The syntax of the if-else                True                  False
                  statement is as follows:                                      Statements in if     Statements in else
                      if (conditional expression):                               block execute         block execute
                              statement(s)
                      else:                                                          Stop
                              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')




                  On running the above program, we get the following output:

                      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 multiple scenarios. First, it checks and evaluates the
                  first condition. If it is true, it will execute the respective statement(s). However, if the condition is

                  false, it moves to the elif statement and evaluates those conditions. Finally, if none of the conditions
                  evaluates to true, it executes the else block. The syntax of if-elif-else statement is as follows:
                      if (conditional expression):
                              statement(s)

                      elif (conditional expression):
                              statement(s)



                  100   Premium Edition-VIII
   97   98   99   100   101   102   103   104   105   106   107