Page 47 - Code & Click - 7
P. 47

NESTED IF … ELSE STATEMENT
            If we need to test multiple conditions and execute different sets of statements depending on the
            evaluation of the conditions in succession, we use the Nested if … else statement.
            A Nested if … else statement includes an if statement immediately after another if statement. The
            first if statement is called the outer if statement, while the if statement
                                                       Start
            under  the  outer  if  statement  is  called  the  inner  if  statement.  The
            inner if statement is executed only when the outer if test condition               Test Condi  on 1  No
                                                                                                   Is
                                                    Input Number
            evaluates to True, else it is bypassed and the control passes to the                  True?
                                                        N
            next set of statements.                                                                 Yes
                                                        Is
                                                            No
            The syntax for using the Nested if … else statement is:
                                                      N > 0 ?
                   if <test condition 1>:                Yes                               No  Test Condi  on 1  Execute
                                                                                                   Is
                     if <test condition 2>:                         Print N is                    True?      else block
                                                      Print N is
                                                                    Nega  ve
                                                      Posi  ve
                       set of statements under if                                                   Yes
                   else:                               Stop                                      Execute
                     set of statements under else                                                if block
                   other statements

                                                                                                 Experiential Learning
                                                        Is
              Lab Activity 3                        Test Condi  on  Yes  Execute
                                                      True?          if block                      Is
             Program for selection of a candidate for a course based on the conditions specified below:       if block 3
                                                                                                         Yes
                                                                                                               Execute
                                                                                               Test Condi  on 1
                                                                                                  True?
             If the candidate is above the age of 18 years and has secured 85 or more marks in the qualifying
                                                        No
             exam, they will be selected for the course, else they will be rejected.                No
             1. Type the code as shown in the image in the Python Editor.
                                                                                   Execute  Yes    Is    Yes   Execute
                                                                                   if block 3  Test Condi  on 2  if block 3
                                                                                                  True?

                                                                                                    No
                                                       Is
                                                   Test Condi  on  Yes  Execute                    Is
                                                      True?        if block                    Test Condi  on 3
                                                                                                  True?
                                                        No
                                                                                                    No
                                                     Execute
                                                     else block                                  Execute
                                                                                                else block
             2. Press the F5 key to execute the code. Sample output of the program is given below.



                                                                                                                           64















                                                                                                                 45
   42   43   44   45   46   47   48   49   50   51   52