Nested If Statements

The code blocks inside the if statement can contain any valid Python code. This means that you can also nest other if statements inside an if statement. For example:

if condition1:
    code_block_1
    
    if condition2:
        code_block_2
    else:
        code_block_3

else:
    code_block_4

which can be illustrated with the control flow diagram:

../../../_images/if_else_nested.png

Fig. 12 Control flow diagram of the pseudo code example of an if statement nested inside an if statement.

Hint

While there are situations which call for nested if statements, always consider whether a series of elifs will serve instead. Nested code blocks can make scripts harder to read.