Page 109 - Code Craft Computer-7
P. 109

Nested if Statement

                                                                          If
            Python allows the nested if     statement. This means the   indented block inside another if...else
                                                     if
            indented block. The second nested   indented block will run only when the first if condition
            evaluates to be true. The syntax of nested if statement is shown below:




            Syntax:
                                                                                           False
            if (Test Expressionl) :                                 Test Expression 1               Statement 1
                 if (Test Expression2) :

                      Indented block 1                                         True
                 else:                                                                     False
                                                                    Test Expression 2               Statement 2
                      Indented block 2
            else:
                                                                               True
                 Indented block 3
                                                                       Statement 3





               Program 5: To find out if the student is selected for the college or not based on the given
               conditions -

               1. Selected if age > 18 and marks > 65

               2. Not selected if age > 18 and marks < 65
               3. Not selected if age < 18

















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




















                                                                109
   104   105   106   107   108   109   110   111   112   113   114