Page 30 - Modular v1.1 Pyhton
P. 30

03                                   CONDITIONAL



                                                                      STATEMENTS IN PYTHON















                         Your Aim
                         to learn about:

                                   if Statement                           if...else Statement
                                    Nested if Statement                      if...elif...else Ladder
                                      Some More Programs



                  There are situations in life when we need to decide based on a situation. For example, if it rains,
                  I will stay at home or else I shall 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.

                  We need to determine which action is to be taken and which statement is to be executed, if the
                  outcome is true or false. In this chapter, we will learn about decision making statements of Python.

                  Decision making in Python is done by using conditional statements which decide the flow of
                  program execution. The following conditional statements are available in Python:

                      if statement
                      if...else statement

                      Nested if statement

                      if…elif…else ladder

                      IF STATEMENT                                                                            False
                                                                                           Test Expression
                  The if statement is the simplest conditional statement. Here,

                  a statement or a collection of statements within the if block
                                                                                                   True
                  is executed only if a certain condition or expression evaluates
                  to True. If the condition evaluates to False, then the control
                                                                                             Body of if
                  of execution is passed to the next statement after the if block.

                  Syntax:
                  if (Test Expression):
                                                                                       Statement just below if
                      Indented statement block
                  # if block ends here


                   28     Touchpad MODULAR (Version 1.0)
   25   26   27   28   29   30   31   32   33   34   35