Page 150 - 2620_Birla Open Mind C-7
P. 150

The if…else Statement

              The  if…else  statement checks for a condition.  If  the
              condition  evaluates to  True, the  indented  block  following      Test Expression
              the if statement is executed, otherwise the indented block
              after the else statement is executed. The syntax of the                                           False
              if…else statement is given below:                                           True

              Syntax:                                                                Body of if          Body of else


              if (Test Expression):

                  Indented block                                                Statement just below
                                                                                      if...else
              else:


                  Indented block
              Program 3: To compare two numbers and print the result.
                   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: 45
                Enter the second number: 65
                Second number is greater than first number


                Enter the first number: 38
                Enter the second number: 22
                First number is greater than second number














                  148  Premium Edition-VII
   145   146   147   148   149   150   151   152   153   154   155