Page 59 - Computer - 7
P. 59

5 5                                         Decision Making
                                                                          Decision Making



                                                                                            in Python
                                                                                            in Python





                Pre-Processing


                      • Conditional Statements                    • If Statement
                      • If … Else Statement                       • Nested If … Else Statement
                      • If … Elif … Else Ladder                   • Indentation of Statements in Python


          The statements in a program are usually executed in sequence. But, sometimes we may need to decide
          between two courses of actions, depending on a specific situation that arises during the execution of
          the program. Decision making is the process of evaluating a given condition and deciding the course of
          action to follow based on the result of the condition.                                Start
          For  example,  deciding  whether  a  person  is  eligible  to  vote  or  note,
                                                                                                                                         Is
          depending on his/her age is an example of decision making. If the age of            Input Number                           Test Condi  on 1  No
                                                                                                                                        True?
          the person is 18 years or above, he/she will be declared eligible to vote,              N
          else he/she will be declared ineligible to vote.                                       Is  No                                   Yes
          Consider the example of accepting a number from the user and deciding                 N > 0 ?
                                                                                                                                         Is
          whether the number is positive or negative. We know that a number is                    Yes                             No  Test Condi  on 1  else block
                                                                                                                                                   Execute
          positive if it is greater than or equal to 0, else it is negative. Thus, we need     Print N is    Print N is                 True?
                                                                                                             Nega  ve
                                                                                                Posi  ve
          to  check  the  condition  to  see  if  the  variable  containing  the  number  is                                              Yes
          greater than 0 or not.                                                                Stop                                   Execute
          The flow chart given in the image illustrates the decision-making involved in the process of checking                         if block
          whether a number is positive or not.                                                    Start
                                                                                                                                            Is
          CONDITIONAL STATEMENTS                                                               Input Number                             Test Condi  on 1  No
                                                                                                 Is
                                                                                                   N
                                                                                                       Yes
          The statements that let us take decisions in a Python program are called Conditional statements. These                           True?
                                                                                                             Execute
                                                                                              Test Condi  on
                                                                                                             if block
                                                                                                True?
                                                                                                                                         Is
          statements work on the basis of specific conditions. The flow of program execution is directed to different                Test Condi  on 1 Yes  Yes  if block 3
                                                                                                                                                    Execute
                                                                                                   Is
                                                                                                       No
          sets of statements on the basis of the evaluation of the condition.                    N > 0 ?                                True?
                                                                                                  No
          Python provides several decision-making statements. Let us learn about them.             Yes                              No    No Is       Execute
                                                                                                                                        Test Condi  on 1  else block
          IF STATEMENT                                                                           Print N is   Print N is                   True?
                                                                                                               Nega  ve
                                                                                                 Posi  ve
                                                                                                                                         Is
                                                                                                                           Execute
                                                                                                                                                    Execute
          The if statement is the simplest conditional statement. It evaluates a test condition. If the test condition    if block 3  Yes  Test Condi  on 2  Yes  if block 3
                                                                                                                                             Yes
                                                                                                                                        True?
                                                                                                  Stop
          evaluates to True, the block of statements under if is executed. If the test condition evaluates to False,                      Execute
          the block of statements under if is bypassed and the control is passed to the statement after the if block.                     if block
                                                                                                                                          No
          The statement used with the if statement is called Selection statements.               Is    Yes  Execute
                                                                                                                                         Is
          The syntax for using the if statement is:                                          Test Condi  on  if block                Test Condi  on 3
                                                                                                True?
                                                                                                  Is
                 if <test condition>:                                                          Test Condi  on  Yes  Execute             True?
                                                                                                  No
                   set of statements                                                             True?         if block                 Test Condi  on 1  Yes  Execute
                                                                                                                                            Is
                                                                                                                                          No
                                                                                               Execute
                 other statements                                                             else block No                             Execute        if block 3
                                                                                                                                           True?
                                                                                                                                       else block
                                                                                                                                             No
                                                                                                              57
                                                                                                                                            Is
                                                                                                                             Execute  Yes         Yes  Execute  64
                                                                                                                             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
                                                                                                                                          else block
                                                                                                                                                                   64
   54   55   56   57   58   59   60   61   62   63   64