| by Arround The Web | No comments

Nested If Statements in Python

The if-conditional statements are the most fundamental concept of any programming language, and they can literally create almost everything in the programming language. While being the most fundamental, if-statements can be used to compute multiple conditions at the same time inside other conditions. Yes, it is true, that is called a nested if statement having conditions inside other conditions.

This post will illustrate the use of nested if-statements with the help of an example. But before that let’s have a look at the syntax of the nested if-statement.

How to Use Nested-if Statements in Python?

The basic structural syntax of nested if statements is defined below:

if (condition_1):
    if(condition_2):
        #body of condition_2
    else:
        #body of else of condition_2
else:
    #body of else block of condition_1

 
In this above syntax:

    • If the condition_1 is True, then it will head inside its body and compute condition_2 (nested).
    • If condition_2 is True, then it will compute its body, otherwise it’s else-block.
    • However, if the first condition is False, then it will head inside the else block of condition_1 and execute its body. This means the nested condition_2 will never be computed at all.

Note: Each condition can have as many “elif” parts as much as required, and the level of nested-if statements can also be as many as required.

Let’s explain the nested-if statements with the help of examples.

Example: Comparing Three Numbers Using Nested-if Statements

In this first example, you will be comparing three numbers and conclude one of the following:

    • All numbers are the same.
    • All numbers are different.
    • Which two numbers are the same and which one is different?

To do this, take the following code:

x = 20
y = 10
z = 10

if (x==y):
    if(x==z):
        print("All numbers are equal")
    else:
        print("X & Y are same, Z is different")
elif(x==z):
    print("X & Z are same, Y us different")
elif(y==z):
    print("Y and Z are same. X is different")
else:
    print("All numbers are unique")

 
In this above code snippet:

    • Three variables x,y,z are given integer values.
    • First, it checks “x” with “z”, if they are the same then it executes the nested if to compute that all numbers are the same.
    • If the nested if returns false, then it computes that only “x” and “y” are the same.
    • Then in the next two elif statements, it compares “x” with “z” and “y” with “z”.
    • If none of the conditions returns true, then the program simply computes that all of the provided numbers are unique.

With the values set at 20,10, and 10 for x, y, and z, the program displays the following output:


However, when the values changed to 15, 15, and 15 for x, y, and z, the output becomes:


This is how the user can utilize the nested-if statements to compute conditions in a hierarchical format.

Conclusion

The nested if-statements are a way to apply conditions in a program through a hierarchical format. This means that an if-condition can be placed inside the body of another if-conditions. There is no limit on the levels of the if-condition hierarchy that can be created. Therefore, you can create as many levels as required for your task, However, be very careful when using multiple levels of nested-if conditions because they can make your program quite complicated.

Share Button

Source: linuxhint.com

Leave a Reply