Computer Science

CBSE Class 11 Computer Science | Python if-elif-else Statement | Notes, Examples & Programs

Class 11 · Computer Science

Python if-elif-else Statement

The if-elif-else statement is a multi-way decision-making statement in Python. It is used when a program has to choose one option from multiple alternatives. Python checks each condition one by one and executes only the block whose condition is True.


Learning Objectives

  • Understand the purpose of the if-elif-else statement.
  • Write programs involving multiple conditions.
  • Differentiate between if, if-else and if-elif-else.
  • Develop logic for solving real-life problems.

What is if-elif-else?

The if-elif-else statement is used when there are more than two possible outcomes. Python evaluates the conditions from top to bottom. As soon as one condition becomes True, its corresponding block is executed and the remaining conditions are skipped.


Syntax

if condition1:
    statements
elif condition2:
    statements
elif condition3:
    statements
else:
    statements

Working of if-elif-else

  1. Python checks the if condition.
  2. If it is True, that block executes.
  3. Otherwise, Python checks the first elif.
  4. This process continues until a True condition is found.
  5. If no condition is True, the else block executes.

Flowchart

           Start
             |
      Condition 1?
        /      \
     Yes        No
     |           |
 Statement   Condition 2?
               /      \
            Yes        No
            |           |
       Statement    Condition 3?
                      /      \
                   Yes        No
                   |           |
              Statement     Else Block
                                |
                               End

Example 1: Check Positive, Negative or Zero

num=int(input("Enter a number: "))
if num>0:
    print("Positive Number")
elif num<0:
    print("Negative Number")
else:
    print("Zero")

Output

Enter a number: -18
Negative Number

Example 2: Find Student Grade

marks=int(input("Enter Marks: "))
if marks>=90:
    print("Grade A")
elif marks>=75:
    print("Grade B")
elif marks>=60:
    print("Grade C")
elif marks>=40:
    print("Grade D")
else:
    print("Fail")

Example 3: Display Day Name

day=int(input("Enter Day Number: "))
if day==1:
    print("Monday")
elif day==2:
    print("Tuesday")
elif day==3:
    print("Wednesday")
elif day==4:
    print("Thursday")
elif day==5:
    print("Friday")
elif day==6:
    print("Saturday")
elif day==7:
    print("Sunday")
else:
    print("Invalid Day")

Example 4: Traffic Signal

signal=input("Enter Signal Colour: ")
if signal=="Red":
    print("Stop")
elif signal=="Yellow":
    print("Wait")
elif signal=="Green":
    print("Go")
else:
    print("Invalid Signal")

Example 5: Electricity Bill Category

units=int(input("Enter Units: "))
if units<=100:
    print("Low Consumption")
elif units<=300:
    print("Medium Consumption")
else:
    print("High Consumption")

Real-Life Applications

  • Student grading system
  • ATM menu selection
  • Income tax calculation
  • Electricity bill calculation
  • Railway reservation system
  • Movie ticket pricing
  • Hospital patient categorisation
  • Banking applications
  • Online shopping discounts
  • Weather advisory systems

Difference between if, if-else and if-elif-else

Feature if if-else if-elif-else
Conditions One One Multiple
Decision Single Two-way Multi-way
elif Used No No Yes
else Required No Yes Optional

Important Points

  • elif means else if.
  • Any number of elif blocks can be used.
  • Only the first True condition executes.
  • Remaining conditions are ignored after a True condition.
  • The else block is optional.
  • Proper indentation is mandatory in Python.

Common Programming Mistakes

1. Incorrect Indentation

Wrong

if age>=18:
print("Eligible")

Correct

if age>=18:
    print("Eligible")

2. Using = instead of ==

Wrong

if age=18:

Correct

if age==18:

3. Incorrect Order of Conditions

Wrong

if marks>=40:
    print("Pass")
elif marks>=90:
    print("Grade A")

Correct

if marks>=90:
    print("Grade A")
elif marks>=40:
    print("Pass")

CBSE Important Programs

  1. Check whether a number is Positive, Negative or Zero.
  2. Find Grade from Marks.
  3. Display Day Name using Day Number.
  4. Display Month Name using Month Number.
  5. Find the Largest among Three Numbers.
  6. Check Leap Year.
  7. Calculate Electricity Bill Category.
  8. Display Traffic Signal Action.
  9. Calculate Income Tax Slab.
  10. Calculate Ticket Price according to Age.

Viva Questions

  1. What is the purpose of the if-elif-else statement?
  2. Can there be multiple elif blocks?
  3. Is the else block compulsory?
  4. Which block executes if multiple conditions are True?
  5. Why is the order of conditions important?
  6. What is indentation?
  7. Can if-elif-else be nested?
  8. What is the difference between if-else and if-elif-else?
  9. What happens if none of the conditions are True and there is no else block?
  10. Give any two real-life applications of if-elif-else.

Exam Tips

  • Use if-elif-else when there are multiple possible outcomes.
  • Arrange conditions from the most specific to the most general.
  • Maintain proper indentation.
  • Test your program with different input values.
  • Remember that only the first True condition executes.

Quick Revision

  • if → Checks one condition.
  • if-else → Chooses between two alternatives.
  • if-elif-else → Chooses one option from multiple alternatives.
  • Python checks conditions from top to bottom.
  • Only one block executes.
  • Use proper indentation for every block.