1.

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 !



Discussion

No Comment Found