Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 138 - 2612_Digimind
P. 138

    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
                                                                                     block execute
                          if (conditional expression):
                                  statement(s)
                                                                                         Stop

                      136   Premium Edition-VII

| 133 134 135 136 137 138 139 140 141 142 143 |