Page 144 - C_GPT _V4 _class_7
P. 144

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







                        CodeGPT (Ver. 4.0)-VII
                142
   139   140   141   142   143   144   145   146   147   148   149