Page 45 - Code & Click - 7
P. 45

5                                Decision Making in Python







                 Pre-Processing
                 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                    Start
            of actions, depending on a specific situation that arises during         Input Number                                       Test Condi  on 1  No
                                                                                                                                             Is
            the execution of the program. Decision making is the process of               N                                                 True?
            evaluating a given condition and deciding the course of action
            to follow based on the result of the condition.                              Is    No                                             Yes
                                                                                        N > 0 ?
            Consider the example of accepting a number from the user and
                                                                                                                                             Is
                                                                                                                                                          Execute
            deciding whether the number is positive or negative. We know                  Yes                                      No   Test Condi  on 1  else block
            that a number is positive if it is greater than or equal to 0, else        Print N is       Print N is                          True?
            it is negative. Thus, we need to check the condition to see if the         Posi  ve         Nega  ve
            variable containing the number is greater than 0 or not.                    Stop                                                  Yes

            The flow chart given in the image illustrates the decision-making               Start                                          Execute
                                                                                                                                           if block
            involved in the process of checking whether a number is positive or not.                                                           Is
                                                                                        Input Number                                      Test Condi  on 1  No
            CONDITIONAL STATEMENTS                                                           N                                               True?
            The statements that let us take decisions in a Python program are called Conditional statements.                                    Yes
                                                                                         Is
                                                                                             Is
            These statements work on the basis of specific conditions.               Test Condi  on  Yes No  Execute
                                                                                           N > 0 ?
                                                                                                         if block
                                                                                        True?
                                                                                                                                             Is
            Python provides several decision-making statements. Let us learn about them.                                                Test Condi  on 1  Yes  Execute
                                                                                                                                               Is
                                                                                                                                                           if block 3
                                                                                              Yes                                    No   Test Condi  on 1  Execute
                                                                                                                                            True?
            IF STATEMENT                                                                  No               Print N is                        True?        else block
                                                                                          Print N is
                                                                                                           Nega  ve
                                                                                           Posi  ve
            The  if  statement  is  the  simplest  conditional  statement.  It  evaluates  a  test  condition.  If  the  test                  No
            condition  evaluates  to  True,  the  block  of  statements  under  if  is  executed.  If  the  test  condition                     Yes
                                                                                            Stop
            evaluates to False, the block of statements under if is bypassed and the control is passed to the                                Execute
                                                                                                                                             Is
                                                                                                                                             if block
                                                                                                                                                            Execute
                                                                                                                           Execute
            statement after the if block.                                                                                 if block 3  Yes  Test Condi  on 2  Yes  if block 3
                                                                                                                                            True?
            The statement used with the if statement is called Selection statements.
            The syntax for using the if statement is:                                                                                          No
                                                                                            Is
                   if <test condition>:                                                 Test Condi  on  Yes  Execute
                                                                                         Is
                                                                                           True?
                                                                                    Test Condi  on  Yes  Execute if block                    Is Is
                     set of statements                                                 True?           if block                         Test Condi  on 3  Yes  Execute
                                                                                                                                          Test Condi  on 1
                                                                                                                                             True?
                                                                                                                                            True?           if block 3
                   other statements                                                          No
                                                                                          No
                                                                                                                                              No No
                                                                                       Execute                   43
                                                                                      else block
                                                                                                                                           Execute
                                                                                                                                               Is
                                                                                                                             Execute  Yes  else block  Yes   Execute
                                                                                                                            if block 3    Test Condi  on 2  if block 3
                                                                                                                                              True?
                                                                                                                                                No
                                                                                                                                                                           64
                                                                                            Is
                                                                                        Test Condi  on  Yes  Execute                           Is
                                                                                                          if block
                                                                                           True?                                          Test Condi  on 3
                                                                                                                                              True?
                                                                                             No
                                                                                                                                                No
                                                                                          Execute
                                                                                         else block
                                                                                                                                             Execute
                                                                                                                                            else block


                                                                                                                                                                           64
   40   41   42   43   44   45   46   47   48   49   50