Elif Statement


 Elif Statement

        Sometimes there are more than two possibilities, in that case we can use the elif statement. It stands for "else-if" which means that if the original if statement is false and the elif statement is true, execute the block of code following the elif statement.


Syntax :

if expression 1:

      Statement(s)

elif expression 2:

      Statement(s)

elif expression 3:

       Statement(s)

else:

       Statement(s)

Working and Execution :

a. If the first if condition is true, the program will execute the body of the if statement. Otherwise, the program will go to the elif block(else if in Python) which basically checks for another if statement. 

b. Again, if the condition is true, the program will execute the body of the elif statement, and if the condition is found to be false, the program will go the next else block and execute the body of the else block.

Flow Chart :


Example : 

  1. number = int(input("Enter the number:"))  
  2. if number==10:  
  3.     print("number is equals to 10")  
  4. elif number==50:  
  5.     print("number is equal to 50");  
  6. elif number==100:  
  7.     print("number is equal to 100");  
  8. else:  
  9.     print("number is not equal to 10, 50 or 100");  


Output :

Enter the number:15
number is not equal to 10, 50 or 100


No comments:

Post a Comment