Page 95 - trackpad v5.1 class 8 flipbook
P. 95

It contains a conditional expression , which is used to compare
                                                                                             Start
                 data. 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,       if condition      False
                 the statements within the if block are not executed. There is no

                 limitation on the number of statements that can appear within                  True
                 the if block. Hence, you may add as many statements in the if        Statements in if block
                 block as needed. The syntax of if statement is as follows:                 execute
                 if (conditional expression):
                     statement(s)                                                             Stop

                 Program 1: To check whether a given number is positive
                     Program1.py

                  File  Edit  Format   Run   Options   Window     Help

                  #Program to check if a number is positive
                  n = int (input ('Enter a number:'))
                  if(n > 0):
                      print('Entered number is positive number')




                     Output

                  Enter a number:35
                  Entered number is positive number






                                         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                                              Start
                 The  if-else statement  is used  to  evaluate  whether a
                 given statement is true or false. If the given condition is
                 true, then statements within the if block are executed.          if condition
                 If  the  given condition  is false, then statements  within                                 False
                 the else block are executed. The syntax of the if-else                True         Statements in else
                 statement is as follows:                                      Statements in if       block execute
                     if (conditional expression):                               block execute
                             statement(s)
                                                                                    Stop




                                                                                        Control Structures in Python  93
   90   91   92   93   94   95   96   97   98   99   100