Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 126 - 2610_Springdales_C-7
P. 126

    THE IF STATEMENT                                                                Start

                  The if statement is the simplest conditional statement. In case of the
                  if statement, a statement or a collection of statements within the if                           False
                  block is executed only if a certain condition or expression evaluates         if condition
                  to  True. If the condition evaluates to  False, then the control of
                  execution  is passed  to the  next  statement after the  if  block.  The            True
                  syntax of the if statement is given below:                                  Statements in if
                                                                                               block execute
                  Syntax:

                  if (Test Expression):                                                           Stop

                      Indented statement block

                  # if block ends here

                           Factbot

                     In Python, the non-zero value is interpreted as True. None and 0 are interpreted as False.

                  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')

                    #Statement outside the if statement
                    print('All numbers greater than 0 are positive')

                  On running the above program, we get the following output:

                       Output

                    Enter a number:6
                    Entered number is positive
                    All numbers greater than 0 are positive

                    Enter a number:-9
                    All numbers greater than 0 are positive

                            Premium Edition-VII
                    124

| 121 122 123 124 125 126 127 128 129 130 131 |