InterviewSolution
| 1. |
Explain nested if—–else statement |
|
Answer» There may be a situation when you want to check for another condition after a condition resolves to true. In such a situation, you can use the nested if construct. In a nested, if construct, you can have an if…elif…else construct inside another if…elif… else construct. Syntax: The syntax of the nested if…elif…else construct may be: if expression1: statement(s) if expression2: statement(s) elif expression3: statement(s) else statement(s) elif expression4: statement(s) else: statement(s) Example: # !/usr/bin/python if var < 200: print “Expression value is less than 200” if var==150: print “=150” elif var = =100: print “=100” elif var ==50: print “=50” elif var < 50 : print “Expression value is less than 50” else : print “Could not find true expression” |
|