Page 94 - Digicode_AI_class_7
P. 94

if…else Statement

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

            if (Test Expression):                                             Body of if           Body of else
                Indented block
            else:

                Indented block                                          Statement just below if




            Program: To compare two numbers and print the result.




















            On running the above program, you will get the following output:
























            Nested if Statement

            Python  allows the  nested if statement.  This  means the  if indented  block inside  another  if…else
            indented block. The second nested if indented block will run only when the first if condition evaluates
            to be true.



                 92    DigiCode AI-VII
   89   90   91   92   93   94   95   96   97   98   99