Page 130 - ComputerGenius_V2.1_Class8
P. 130

Output 2:
                    Enter a number : -4
                    -4 is a negative number.

                    This is outside if-else.
                  Description:
                  In the above program, two different outputs have been shown for the execution of both ‘if’ and ‘else’
                  Statement.

                  if...elif...else Statement

                  The if…elif…else ladder is another type of if statement. It helps us to test multiple conditions and
                  follows a top-down approach. The ‘elif’ is short for ‘else if’.

                  l    If the condition for ‘if’ is False, it checks the condition of the next ‘elif’ block and so on.

                  l    If all the conditions are False, body of ‘else’ is executed.
                  Syntax:

                    if (Condition1):
                      Statement1

                      Statement2
                      . . . . . . . . . .

                    elif(Condition2):
                      Statement3

                      Statement4
                      . . . . . . . . . .

                    elif(Condition3):
                      Statement5

                      Statement6
                      . . . . . . . . . .

                    else :
                      Statement7
                      Statement8
                    . . . . . . . . . .

                  Example 5: if-elif-else Statement
                  Program:
                    num = 3.4

                    if num > 0:
                      print(cnumber.")
                    elif num ==0:



                      128    Computer Genius (V2.1)-VIII
   125   126   127   128   129   130   131   132   133   134   135