Skip to content
Computer ScienceCBSE Class 11 · Flow of Control

if, elif and else in Python Class 11: Decision Programs and Output Tracing

Updated 17 October 2026 · Fact-checked

The if statement runs a block only when its condition is True; else runs when the condition is False; elif adds further conditions to a single chain in which only the first true branch runs. Blocks are marked by indentation. Separate if statements are all tested independently, but an if-elif-else chain stops after the first True condition.

Tests run top to bottom; for 82 the first True test (marks >= 75) gives grade B, and the faded branches below never run.

Illustration drawn for QuestionBuddy.

Understand if, elif and else in Python: Decision Programs and Output Tracing

Syntax.

if condition:
    statements
elif condition2:
    statements
else:
    statements

The colon and the indentation (usually 4 spaces) are required. A condition uses relational operators (==, !=, <, >, <=, >=) and logical operators (and, or, not).

if-elif-else is one decision. Conditions are tested top to bottom; as soon as one is True its block runs and the rest are skipped.

Separate ifs are independent.

x = 15
if x % 3 == 0:
    print("A")
if x % 5 == 0:
    print("B")
else:
    print("C")

Output: A then B (two lines). The first if is True, and the second if-else is a separate decision: 15 % 5 is 0, so B is printed. With elif x % 5 == 0 instead of the second if, only A would be printed.

Grade program.

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

For input 82 the output is Grade: B; for 90 it is Grade: A; for 59 it is Grade: D. Order matters: the largest range is tested first.

Leap year. A year is a leap year if divisible by 400, or divisible by 4 but not by 100.

year = int(input("Year: "))
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
    print("Leap year")
else:
    print("Not a leap year")

2000 and 2024 print Leap year; 1900 and 2023 print Not a leap year.

Nested if. An if inside another if/else block. Use it when the second decision depends on the first.

Operator precedence in conditions. not is higher than and, which is higher than or. 5 > 3 and 2 > 4 or 1 == 1 is (True and False) or True, which is True.

if, elif and else in Python: Decision Programs and Output Tracing: key facts

Structure
  • if condition:
  • elif condition:
  • else:
  • Colon and indentation are required
Only one branch of a chain runs.
Relational operators
  • == equal
  • != not equal
  • < less
  • > greater
  • <= and >= with equality
Result is True or False.
Logical operators
  • not is highest
  • and next
  • or is lowest
  • Brackets override
Use brackets for clarity.
Leap year
Divisible by 400 | or divisible by 4 and not by 100
2000: leap; 1900: not.

How to answer if, elif and else in Python: Decision Programs and Output Tracing questions

To trace a decision program, follow the first true branch only.

  1. 1Write the value of each variable before the first if.
  2. 2Evaluate the condition and write True or False.
  3. 3In an if-elif chain stop at the first True; separate ifs are all tested.
  4. 4Write exactly the output including spaces and the order of print statements.
  5. 5For input() programs, remember the input is a string; convert with int() before comparing numbers.

if, elif and else in Python: Decision Programs and Output Tracing in real life

The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

A ticket price

Suppose a museum charges 0 for ages below 5, 50 for ages 5 to 17 and 120 for 18 and above.

Write the decision and the price for age 17.

Try it first, then show the solutionSolution
  1. if age < 5: price 0; elif age < 18: price 50; else: price 120.
  2. For 17, age < 5 is False and age < 18 is True.

Answer: Price is 50.

Memory hook: Check the smallest range first.

Tricks to remember if, elif and else in Python: Decision Programs and Output Tracing

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • One chain, one branch

    In if-elif-else only the first True branch runs.

    Use it when: Chains.

  • Divisible by 4 not 100 or by 400

    Leap year rule in one line.

    Use it when: Leap year.

if, elif and else in Python: Decision Programs and Output Tracing: solved examples

Ordered from board level to HOTS. Try each one before you open the solution.

Example 1: Output of a chain · 1 mark · MCQBoard level

What is the output of the code?

x = 7
if x > 5:
    print("big")
elif x > 2:
    print("medium")
else:
    print("small")

(a) big
(b) medium
(c) big medium
(d) small

Try it first, then show the solutionSolution
  1. 7 > 5 is True, so the first branch runs and the rest are skipped.

Answer: (a) big

Example 2: Grade · 3 marks · Short answerBoard level

Trace the grade program in the concept for the inputs 82, 90 and 59 and write the outputs. (3 marks)

Try it first, then show the solutionSolution
  1. 82: 82 >= 90 is False; 82 >= 75 is True: grade B. Output: Grade: B.
  2. 90: first condition True: Grade: A.
  3. 59: all conditions False: else gives D. Output: Grade: D.

Answer: Grade: B, Grade: A, Grade: D.

Example 3: Leap year · 4 marks · Short answerHarder board level

Write a program to input a year and print whether it is a leap year. State the output for 1900, 2000, 2024 and 2023. (4 marks)

Try it first, then show the solutionSolution
  1. Use: if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0): print("Leap year") else: print("Not a leap year").
  2. 1900: 1900 % 400 = 300, 1900 % 4 = 0 but 1900 % 100 = 0, so Not a leap year.
  3. 2000: 2000 % 400 = 0: Leap year. 2024: divisible by 4, not by 100: Leap year.
  4. 2023: not divisible by 4: Not a leap year.

Answer: 1900 Not a leap year; 2000 Leap year; 2024 Leap year; 2023 Not a leap year.

Example 4: if versus elif · 4 marks · Short answerHarder board level

What is the output of both programs?
Program 1:

x = 15
if x % 3 == 0:
    print("A")
if x % 5 == 0:
    print("B")
else:
    print("C")

Program 2:

x = 15
if x % 3 == 0:
    print("A")
elif x % 5 == 0:
    print("B")
else:
    print("C")

Explain the difference. (4 marks)

Try it first, then show the solutionSolution
  1. Program 1: x % 3 == 0 is True, prints A. The second if is separate: x % 5 == 0 is True, prints B. Output: A then B on two lines.
  2. Program 2: x % 3 == 0 is True, prints A and skips the rest. Output: A.
  3. In Program 1 two separate decisions are made; in Program 2 only one branch of a single chain runs.

Answer: Program 1: A and B; Program 2: A.

Example 5: Largest of three · 5 marks · Short answerHOTS / competency

Trace this code and give its output:

a, b, c = 14, 37, 25
if a >= b and a >= c:
    big = a
elif b >= c:
    big = b
else:
    big = c
print("Largest:", big)

Then state what happens if a = b = c = 5, and why the program is still correct. (5 marks)

Try it first, then show the solutionSolution
  1. a >= b is 14 >= 37, False, so the first condition is False.
  2. b >= c is 37 >= 25, True, so big = 37.
  3. Output: Largest: 37.
  4. If all are 5, a >= b and a >= c is True and big = 5, which is correct because they are equal.

Answer: Largest: 37; for equal values it gives 5, correct.

Where students lose marks in if, elif and else in Python: Decision Programs and Output Tracing

The slips examiners see most often, and what to write instead.

  • Mistake: Forgetting the colon

    Treating it like English.

    Wrong working

    if x > 5

    Right working

    if x > 5:

    Do this: Every if, elif, else needs a colon.

    Usually costs: 1 mark

  • Mistake: Comparing the string from input()

    Not converting.

    Wrong working

    marks = input(); if marks >= 90:

    Right working

    marks = int(input()); if marks >= 90:

    Do this: input() returns a string.

    Usually costs: 1 mark

  • Mistake: Using = in a condition

    Confusing assignment and comparison.

    Wrong working

    if x = 5:

    Right working

    if x == 5:

    Do this: == compares; = assigns.

    Usually costs: 1 mark

Exam tips

  • Write the output exactly as printed, including capitalisation.
  • Underline the first True condition in tracing questions.
  • Mention the indentation rule in theory questions.
  • Use brackets in complex conditions.

Practice questions on if, elif and else in Python: Decision Programs and Output Tracing

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    In an if-elif-else chain, how many branches can run in one execution?
    (a) all
    (b) exactly one
    (c) at least two
    (d) none

    Hint

    First True only.

    Answer

    (b) exactly one

  2. Q2 · 3 marks · Short answerHarder board level

    What is the output?

    n = -4
    if n > 0:
        print("positive")
    else:
        if n == 0:
            print("zero")
        else:
            print("negative")

    (3 marks)

    Hint

    Nested else.

    Answer

    n > 0 is False, so the else block runs. n == 0 is False, so the inner else runs. Output: negative.

  3. Q3 · 4 marks · Short answerHarder board level

    Write a program that reads three sides of a triangle as integers and prints Equilateral, Isosceles or Scalene. Give the output for 5, 5, 5, for 5, 5, 8 and for 3, 4, 5. (4 marks)

    Hint

    Check equal sides.

    Answer

    if a == b == c: print("Equilateral") elif a == b or b == c or a == c: print("Isosceles") else: print("Scalene"). Outputs: Equilateral, Isosceles, Scalene.

  4. Q4 · 1 mark · Assertion-reasonHarder board level

    Assertion (A): In Python, the block of an if statement is identified by indentation.
    Reason (R): Python does not use curly brackets to mark a block.
    (a) Both A and R are true, and R is the correct explanation of A.
    (b) Both A and R are true, but R is not the correct explanation of A.
    (c) A is true, but R is false.
    (d) A is false, but R is true.

    Hint

    Python style.

    Answer

    (a) Both A and R are true, and R is the correct explanation of A.

  5. Q5 · 5 marks · Short answerHOTS / competency

    Find and correct the errors in this program and give the output for input 70:

    marks = input("Marks: ")
    if marks >= 75
        print("Distinction")
    elif marks >= 40:
    print("Pass")
    else:
        print("Fail")

    (5 marks)

    Hint

    Three faults: type conversion, a missing colon, and indentation.

    Answer

    Three faults: (1) input() must be converted: marks = int(input("Marks: ")); (2) missing colon after the first condition; (3) the print under elif is not indented. Corrected, the input 70 gives: 70 >= 75 False; 70 >= 40 True; output Pass.

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.Which keyword adds another condition?Show answer

    elif.

  2. 2.What marks a block in Python?Show answer

    Indentation.

  3. 3.Does elif run if the if is True?Show answer

    No.

  4. 4.Leap year rule?Show answer

    Divisible by 400, or by 4 but not by 100.

  5. 5.What type does input() return?Show answer

    A string.

if, elif and else in Python: Decision Programs and Output Tracing: frequently asked questions

Can there be an elif without an if?

No; an elif must follow an if.

Is else compulsory?

No, it is optional.

What is a nested if?

An if statement inside another if or else block.