The nested if Statement
The nested if statement
Sometimes above discussed forms of if are not enough. You may need need to test additional conditional. For such situation, python also supports nested-if-form of if.
A nested if is an if that has another if in its if’s body or in elif’s body or in its else’s body.
The nested if can have one o the following forms:
From 1 (if inside if’s body) If < conditional expression >: If <conditional expression>: Statement (s) else: statement (s) elif <conditional expression>: statement [statements] else: statement [statements] | From 2 (if inside elif’s body) If < conditional expression >: statement [statements] elif <conditional expression>: If < conditional expression >: Statement(s) else: Statement(s) else: statement [statements] |
From 3 (if inside else’s body) If < conditional expression >: statement [statements] elif <conditional expression>: statement [statements] else: If < conditional expression >: Statement(s) else: Statement(s)
|
From 4 (if inside if’s as well as else’s or elif’s body, i.e., multiple ifs inside) If < conditional expression >: If <conditional expression>: Statement (s) else: statement (s) elif <conditional expression>: If <conditional expression>: statement (s) else: statement (s) else: If <conditional expression>: statement (s) else: statement (s) |
In a nested if statement, either there can be if statement(s) in its body-of=if or not in its body-of-elif or in any two of these or in all of these. Recall that you used nested-if unknowingly in program 8.3 isn’t that superb? ;