HomeLinuxNested If Statements in Python

Nested If Statements in Python


The if-conditional statements are essentially the most basic idea of any programming language, and so they can actually create virtually every little thing within the programming language. Whereas being essentially the most basic, if-statements can be utilized to compute a number of circumstances on the similar time inside different circumstances. Sure, it’s true, that is named a nested if assertion having circumstances inside different circumstances.

This submit will illustrate using nested if-statements with the assistance of an instance. However earlier than that allow’s take a look on the syntax of the nested if-statement.

How you can Use Nested-if Statements in Python?

The essential structural syntax of nested if statements is outlined beneath:

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

 
On this above syntax:

    • If the condition_1 is True, then it would head inside its physique and compute condition_2 (nested).
    • If condition_2 is True, then it would compute its physique, in any other case it’s else-block.
    • Nevertheless, if the primary situation is False, then it would head contained in the else block of condition_1 and execute its physique. This implies the nested condition_2 won’t ever be computed in any respect.

Notice: Every situation can have as many “elif” components as a lot as required, and the extent of nested-if statements will also be as many as required.

Let’s clarify the nested-if statements with the assistance of examples.

Instance: Evaluating Three Numbers Utilizing Nested-if Statements

On this first instance, you’ll be evaluating three numbers and conclude one of many following:

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

To do that, take the next code:

x = 20
y = 10
z = 10

if (x==y):
    if(x==z):
        print(“All numbers are equal”)
    else:
        print(“X & Y are similar, Z is totally different”)
elif(x==z):
    print(“X & Z are similar, Y us totally different”)
elif(y==z):
    print(“Y and Z are similar. X is totally different”)
else:
    print(“All numbers are distinctive”)

 
On this above code snippet:

    • Three variables x,y,z are given integer values.
    • First, it checks “x” with “z”, if they’re the identical then it executes the nested if to compute that each one numbers are the identical.
    • If the nested if returns false, then it computes that solely “x” and “y” are the identical.
    • Then within the subsequent two elif statements, it compares “x” with “z” and “y” with “z”.
    • If not one of the circumstances returns true, then this system merely computes that the entire offered numbers are distinctive.

With the values set at 20,10, and 10 for x, y, and z, this system shows the next output:


Nevertheless, when the values modified to fifteen, 15, and 15 for x, y, and z, the output turns into:


That is how the consumer can make the most of the nested-if statements to compute circumstances in a hierarchical format.

Conclusion

The nested if-statements are a option to apply circumstances in a program via a hierarchical format. Which means that an if-condition will be positioned contained in the physique of one other if-conditions. There isn’t any restrict on the degrees of the if-condition hierarchy that may be created. Due to this fact, you possibly can create as many ranges as required to your activity, Nevertheless, be very cautious when utilizing a number of ranges of nested-if circumstances as a result of they’ll make your program fairly sophisticated.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments