Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 102 - 2607_Trackpad_Pro_5.1_flipbook_C7
P. 102

    There is no limitation on the number of statements that can appear within the if block. Hence,
                     you may add as many statements in the if block as needed. All the statements must be indented
                     properly. The syntax of the if statement is as follows:

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

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

                                                                                            Control Structures in Python  101

| 97 98 99 100 101 102 103 104 105 106 107 |