Page 107 - Trackpad_V5_Book 8
P. 107

It contains a conditional expression using which data is compared. A decision is made based on
            the result of the comparison. If the result of the expression is true, the statements within the if
            block are executed and if the result of the expression is false, the statements within the if block are

            not executed. There is no limitation on the number
            of statements that can appear under the if block.
            Hence, you may add as many statements in the if              Condition:      True
            block as you need. The syntax of if statement is as         if(age>=18)             Allowed to vote
            follows:
            if (conditional expression):                                False

                statement(s)
            Program 1: To check your eligibility to vote.

















            In the above example, the conditional statement will always evaluate the result as True because
            the value of x is greater than 18. Hence, the output of the above code would be ‘You are eligible
            to cast your vote’.



                                    Subject: If statement execution criteria
                                    A  statement  or statements  indented  within  an if statement  is  executed
                                    only if the condition is true, otherwise the control skips the if statement and
                                    moves on to the statements outside the if block.




            THE if-else STATEMENT

            The if-else statement is used to evaluate whether
            a  given statement  is  true  or false. If the  given       Condition:      True
            condition  is true, the  if block  is  executed.  If  the                               Equal
            given condition is false, the else block is executed.       if(a==b)
            The syntax of the if statement is as follows:
                if (conditional expression):                           False
                        statement(s)                                    Not Equal
                else:
                        statement(s)






                                                                                   Control Structures in Python  105
   102   103   104   105   106   107   108   109   110   111   112