Page 132 - TP_Plus_v4_Class7
P. 132

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














                  130  Plus (Ver. 4.0)-VII
   127   128   129   130   131   132   133   134   135   136   137