Python if else and nested if else statements


Gaming Laptop Series

In programming conditional statements provides a way in which the computer takes a decision based on whether a condition is true or false. The if and else statements are common among various programming languages.

In this article, you will see the usage of if-else and nested if-else statements in Python.

The if-else statement in Python

Using if-else statements program takes decision what to do when a condition is true or false. The syntax of if-else is given below.

You can use if without using else statement in the following way –

if condition:
            statement(s)

Here if the condition is true the statement(s) under it will be executed otherwise execution control will pass to the next statement in the code.

The syntax of the if-else statement in Python is given below.

if condition:
     statement1
else:
     statement2

How if-else statement works in Python

First, the if condition is checked it either return True or False, now when –

  • The condition is True the statement(s) under the if will be executed
  • Otherwise, statement(s) under else will be executed

Example of using if-else statement

The example of using an if-else statement is given below.

num=input("Enter a number: ")
num=int(num)
if num>=10:
      print("The number is greater than or equal to 10")
else:
      print("The number is less than 10")

The output will look like this –

The if-elif-else statement in Python

The elif is the short form of else if, it allows us to check multiple conditions in Python. The syntax of the if-elif-else statement is given below –

if condition1:
    statement(s)
elif condition2:
    statement(s)
elif condtion3:
    statement(s)
else:
    statement(s)

How does it work in Python?

First, the condition of the if statement will be evaluated –

  • If it returns True the block under the if statement will be executed
  • If it returns False the interpreter will move to the elif statement and its condition will be checked if it returns True the statements under this condition will be executed otherwise the interpreter move to the next elif or else the statement
  • The process will continue till all the condition checked or it finds a condition True meanwhile

Example of using if-elif-else statement

The following example shows the use of the if-elif-else statement.

num=input("Enter a number: ")
num=int(num)
if num>0:
     print("The given number is greater than zero")
elif num<0:
     print("The given number is less than zero")
else:
     print("The given number is equal to zero")

Now based on the user input different conditions will be checked if it finds a condition true the statement under that will be executed otherwise statement under else will be executed.

Nested if-else statement

You can use if-else or if-elif-else statements under if-else or if-elif-else statements this is called nesting. The indentation is used to separate these blocks. An example of a nested if-else statement is given below.

num=5
if num<10:
        if num==5:
             print("The number is equal to 5")
        else:
             print("The number is less than 10")
else:
     print("The number is greater than 10")

When you execute this you will see –

Now write and execute the code on your system so that you can understand how it is working. If you find any difficulty then write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.