Nested if statements


Nested if statements

The “If” statement that written inside an “if” statement is called a nested if statement. Sometimes you may have to check for the situation inside the if statement after the “if” condition becomes true. You can use a nested if statement in these cases. “nested if” statement is used when the decision of the second statement depends on the statement of the first statement.
You can use if, elif, and else statement inside the if statement. This will also count towards the nested if statement.

E.g., 
if(condition1) {
	if(condition2) {
	} elif (condition3) {
	} else {
	}
}

Any number of these statements can be put inside one another. Indentation is the only way to figure out the spaces in the nesting level, which means multiple if-else statements inside other if-else statements.

Example No.1:

x = 41 if x > 10: print("Knowledge2life website") if x > 20: print("Knowledge2life 1") else: print("Knowledge2life 2")

OUTPUT:

Knowledge2life website
Knowledge2life 1

Example No.2:

x = 41 if x > 10: print("Knowledge2life website") if x > 20: print("Knowledge2life 1") if x > 50: print("Knowledge2life 2") else: print("Knowledge2life 3")

OUTPUT:

Knowledge2life website
Knowledge2life 1
Knowledge2life 3