MENU
Question -

Explain тАЬelif statementтАЭ with example.



Answer -

elif statement : The elif statement allows you to check multiple expressions for truth value and execute a block of code as soon as one of the conditions evaluates to true. Like the else, the elif statement is optional. However, unlike else , for which there can be at most one statement, there can be an arbitrary number of elif statements following an if
The syntax of the ifтАжelif statement is
If expression1 :
statement (s)
elif expression2 :
statement(s)
elif expression3 :
statement(s)
else:
statement(s)
Example
# !/usr/bin/py thon
var=100
if var = = 200 :
print тАЬ1-Got a true expression valueтАЭ
print var
elif var==150:
print тАЬ2-Got a true expression valueтАЭ
print var2
elif var ==100:
print тАЬ3-Got a true expression valueтАЭ
print var
else:
print тАЬ4-Got a false expression valueтАЭ
print var
print тАЬGood bye!тАЭ
when the above code is executed, it produces the following output :
3- Got a true expression value
100
Good bye !

Comment(S)

Show all Coment

Leave a Comment

Free - Previous Years Question Papers
Any questions? Ask us!
×