Page 61 - Computer - 7
P. 61

2. Press the F5 key to execute the code. Sample output of the program is given below.






















          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,
                                                         Start
          while the if statement under the outer if statement is called the inner if                 Is    No
                                                      Input Number
          statement. The inner if statement is executed only when the outer if test             Test Condi  on 1
                                                                                                   True?
                                                          N
          condition evaluates to True, else it is bypassed and the control passes to
          the next set of statements.                   N > 0 ?  No                                   Yes
                                                          Is
          The syntax for using the Nested if … else statement is:
                                                                                                     Is
                                                                                                               Execute
                 if <test condition 1>:                   Yes                                No  Test Condi  on 1  else block
                                                       Print N is
                   if <test condition 2>: Posi  ve                    Print N is                   True?
                                                                      Nega  ve
                     set of statements under if                                                       Yes
                 else:                                   Stop                                      Execute
                   set of statements under else                                                    if block
                 other statements


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




                                                                                                                             64
   56   57   58   59   60   61   62   63   64   65   66