Nested Conditions

Nesting is the act of placing programming statements inside another. We can nest if statements to create complex decision trees in our program.

if statements within our if statements

# Example Code Format
    if boolean_condition1:
        # code here

        if boolean_conditionA:
            # code here
        else:
            # code here

    elif boolean_condition2:
        # code here

        if boolean_conditionB:
            # code here
        elif boolean_conditionC:
            # code here
    else:
        # code here
        if boolean_conditionD:
            # code here

This type of conditional statements are called nested conditional statements.

  • When a certain condition is True, we can check upon more conditions for the program

  • This is only required when the complexity of the condition increases depending on the given problem

Nested vs Logical Operator

The following two python conditional formats are equivalent in execution.

Last updated