if–elif–else Statement

The if–elif–else statement is used to check multiple conditions in a program.
It allows more than two choices, making decision-making easier.

The term elif means “else if”, and it helps to check conditions one by one.

When the program runs, it executes the first True condition and skips the remaining ones.

Explanation 

  • The program starts with if
  • If the condition is False, it checks the elif condition
  • It keeps checking conditions one by one
  • Stops when it finds a True condition
  • If none are True, the else block runs
  • In the end, only one block executes


Real-Time Scenarios

  • Marks grading (A, B, C, Fail)
  • Traffic signal (Red, Yellow, Green)
  • Weather check (Hot, Cold, Rainy)
  • Age group (Child, Teen, Adult)
  • Game levels (Easy, Medium, Hard)

Syntax

if condition1:
    # block 1
elif condition2:
    # block 2
else:
    # default block

Example 1

marks = 75

if marks >= 90:
    print("A Grade")
elif marks >= 75:
    print("B Grade")
elif marks >= 50:
    print("C Grade")
else:
    print("Fail")

Output:

B Grade

Example 2

time = 14

if time < 12:
    print("Morning")
elif time < 18:
    print("Afternoon")
else:
    print("Evening")

Output:

Afternoon

Why if–elif–else is Important

  • Handles multiple conditions
  • Improves decision-making logic
  • Used in real-world applications
  • Makes programs more efficient

Common Beginner Mistakes

  •  Wrong order of conditions
  •  Missing else block
  •  Incorrect indentation
  • Using overlapping conditions


➤ Key Points

  • Can have multiple elif blocks
  • Checks conditions top to bottom
  • Stops when first True condition is found

➤ Tips
  • Arrange conditions in logical order
  • Avoid unnecessary elif statements
  • Always include else (default case)

Check your knowledge

Quickly verify what you've learned from this tutorial.

Question 1

What does elif mean?

elif means else if.

Question 2

How many blocks run in if–elif–else?

Only the first True condition block executes.

Question 3

What is the output?

marks = 40

if marks >= 90:
    print("A")
elif marks >= 50:
    print("B")
else:
    print("Fail")

40 does not satisfy conditions → else runs.

Question 4

What is the output?

x = 15

if x > 20:
    print("A")
elif x > 10:
    print("B")
else:
    print("C")

First is False, second is True → prints B.

Question 5

What is the output?

x = 25

if x > 10:
    print("A")
elif x > 20:
    print("B")
else:
    print("C")

First condition is True, so it stops and prints A.

Congratulations!

You've successfully mastered the knowledge check for "if–elif–else Statement."

For more questions and practice, click the link below:

Practice More Questions
Previous Topic if–else Statement Next Topic Nested if Statements