Nested if statements in python

Spread the love

A statement that contains other statement is called a compound statement. To perform more complex checks, if statements can be nested that is placed one inside the other. In such a case, the inner if statement is the statement part of the outer one. Nested if statements are used to check if more than one condition is satisfied.

Consider the code given below to understand this concept.

num = int(input("Enter any number from 0-30: "))
if (num >= 0 and num < 10):
    print("It is in the range 0-10")
if (num >= 10 and num < 20):
    print("It is in the range 10-20")
if (num >= 20 and num < 30):
    print("It is in the range 20-30")
Enter any number from 0-30: 25
It is in the range 20-30

Rating: 1 out of 5.

Leave a Reply