of if Test Expression of elif Body of if Body of elif Body of else false false True True Fig . if..elif..else statement execution ‘ Multiple if..else statements can be combined to one if..elif…else. ‘elif’ can be considered to be abbreviation of ‘else if’. In an ‘if’ statement there is no limit of ‘elif’ clause that can be used, but an ‘else’ clause if used should be placed at the end.
if..elif..else statement is similar to nested if statement which you have learnt in C++. Note 12th Computer Chapter - - Control Structures Average Grade >= and above A >= and < B >= and < C >= and < D Otherwise E m1=int (input(“Enter mark in first subject : ”)) m2=int (input(“Enter mark in second subject : ”)) avg= (m1+m2)/ if avg>= : print (“Grade : A”) elif avg>= and avg< : print (“Grade : B”) elif avg>= and avg< : print (“Grade : C”) elif avg>= and avg< : print (“Grade : D”) else: print (“Grade : E”) Output : Enter mark in first subject : Enter mark in second subject : Grade : D Output : Enter mark in first subject : Enter mark in second subject : Grade : B Example . : #Program to illustrate the use of nested if statement In the above example of if and elif statement are both indented four spaces, which is a typical amount of indentation for Python . In most other programming languages, indentation is used only to help make the code look pretty.
But in Python , it is required to indicate to which block of code the statement belongs to. Note 12th Computer Chapter - - ch=input (“Enter a character :”) # to check if the letter is vowel if ch in (‘a’, ‘A’, ‘e’, ‘E’, ‘i’, ‘I’, ‘o’, ‘O’, ‘u’, ‘U’): print (ch,’ is a vowel’) # to check if the letter typed is not ‘a’ or ‘b’ or ‘c’ if ch not in (‘a’, ’b’, ’c’): print (ch,’ the letter is not a/b/c’) Output : Enter