Editor choice

nested if statement in python? if | if-else | if-elif Examples

A statement when executed, on the basis of the result of a condition given in the program code known as the conditional statement

firstly In python, there are different forms of if statement they are

  • if
  • if-else
  • if-elif-else

we have discussed if and if-else in detail

If-elif-else condition

In some cases, we want to check the condition at the else case also, ie, condition given in

the form of else if. To help in such conditions, Python provides if-elif and if-elif-else

statements. let’s discuss more clearly but it in the following content

Syntax

if <conditional expression> :
    statement
    [statements]
elif <conditional expressions :
    statement
    [statements]

Explanation

If keyword should be given followed by the condition and ending with a colon :

Firstly the if condition will check for the condition given to it and if the given condition satisfied then it will execute the statements given inside

how if-elif works

An if statement checks for a particular condition and

if the condition results to be true,

then a bunch of statements or set-of-statements below them executed.

in case the condition is false, it will check the conditions given in the elif

if it is also satisfied then it will execute the statements given below the elif condition

in case the elif condition also fails then it will move to the else part and then it will proceed further

Flowchart of Elif

EXAMPLE

a=55.25
if a > 0:
    print("Positive number")
elif a == 0:
    print("Zero")
else:
    print("Negative number")

Output

positive number 

Explanation

Firstly we are checking if the condition is true or not

yes a is greater than 0 so it will come outside the loop and starts executing the statements given

in this example, we have given the statements to print if it is greater than zero as positive

so it printed positive number

Nested If (elif-else) condition

In some cases we need to check additional conditions. In such situations, Python also

supports a nested-if form. The 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.

Syntax

if <conditional expression1> :
    if <conditional expression2> :
        statement
        [statements]
    elif <conditional expressions :
        statement
        [statements]
    else
        statement
        [statements]
else <conditional expressions :
    statement
    [statements]

how if-elif works

An if statement checks for a particular condition and

if the condition results to be true,

then again it will check for the inner if condition

if it fails it will check for inner elif below it

even it also fails then inner else statements will

be executed

in case if the condition fails then it will move to the else part and then it will proceed further

Flowchart of Nested if

EXAMPLE

a = float(input("Enter a number: "))
if a >= 0:
    if a == 0:
        print("Zero")
    else:
        print("Positive number")
else:
    print("Negative number")

Output

Enter a number: -9
Negative number

Explanation

Firstly we are checking if the condition is true or not

no its a is lesser than 0 so it will come outside to else statement and starts executing the statements given

in this example, we have given the statements to print if it is lesser than zero as negative

so it printed negative number

We will be happy to hear your thoughts

Leave a reply

Edusera
Logo
Open chat
1
Scan the code
Hello!👋
Can we help you?