Page 91 - TP_Modular_V2.1_Class7
P. 91

CONDITIONAL


                    09                                                STATEMENTS IN PYTHON














                                      Your Aim

                                      to learn about:

                                             Decision Making Statements       The if Statement
                                              The if…else Statement             Nested if Statement
                                               The if…elif…else Ladder

                 There  are  situations  in life where  we  must make decisions based  on  the  circumstances.  For

                 example, if it rains, we will stay at home, or else we will visit the market. Similar situations arise
                 in programming as well, where we need to make some decisions and, based on these decisions,
                 execute a set of commands. Whether the outcome is true or false, we must decide which action to
                 take and which statement to execute. In this chapter, we will learn about conditional statements
                 in Python.

                    DECISION MAKING STATEMENTS
                                                                                             Start
                 Conditional statements, also known  as decision-making or
                 control flow statements, are used in programming to execute
                                                                                                             False
                 different  blocks of code based on certain  conditions. The               if condition

                 following Decision Making Statements are available in Python:
                                                                                                True
                    if statement
                                                                                      Statements in if block
                    if...else statement                                                     execute

                    Nested if statement
                                                                                             Stop
                    if…elif…else ladder
                    THE IF STATEMENT

                 The if statement is the simplest conditional statement. In case of the if statement, a statement or
                 a collection of statements within the if block is executed only if a certain condition or expression
                 evaluates to True. If the condition evaluates to False, then the control of execution is passed to
                 the next statement after the if block. The syntax of the if statement is given below:

                 Syntax:

                 if (Test Expression):

                                                                           Conditional Statements in Python       89
   86   87   88   89   90   91   92   93   94   95   96