Concepts / Chaining Conditions with elif

Chaining Conditions with elif

An if-else statement creates two mutually exclusive branches: one executes if the condition is true, the other if it is false.

  • Programming
Interactive lab

Try it: if / elif / else

How Python tests if / elif conditions from the top, runs only the first true branch, and how and / or stop evaluating as soon as the answer is known (short-circuiting).

How it works

  1. Python evaluates the if condition first.
  2. If it is False, it tries each elif in order; the first True one runs and the rest are skipped without being tested.
  3. If none is True, the else branch runs.
  4. and stops at the first False, or stops at the first True — the right side may never be evaluated.

Default run (5 steps): score = 84; A = 90, B = 80, C = 70. Python tests the conditions from the top. … print(grade) shows B.

Educational simulation

Loading the simulation…

Choosing a Path

Programs often need to respond differently depending on a condition. An if-else statement creates two mutually exclusive branches: one runs when the condition is true, and the other runs when the condition is false. Chaining conditions with elif extends this alternative-execution idea so a program can organize several possible tests instead of treating every situation as only true or false.

ConditionTrue branchContinueFalse branchtruefalseafter branchafter branch
How does one condition split execution into alternative paths, and why does an if-else statement run only one branch?

Reading a Condition Chain

A basic if-else statement has two possible branches. The condition is evaluated first. If it is true, the indented statements under if run. If it is false, the indented statements under else run. Because a condition must be either true or false, exactly one of these two branches executes.

An elif chain applies the same alternative-execution reasoning to several conditions. The program checks the conditions in their written order. When a condition matches, its branch is selected and the remaining alternatives are not selected for that decision. An else branch provides the remaining alternative when none of the preceding conditions matches.

if conditionif branchelif branchContinueelif conditionelse branchtruefalsetruefalseselected pathselected pathselected path
Which condition is tested next, and where does execution stop after the first matching condition?

Tracing a Selection

Worked example

Following the First Matching Branch

Trace the condition chain for temperature = 18 and determine which message is produced.

  1. 1
    Start with the if condition

    The value 18 is compared with 30. The condition 18 >= 30 is false, so the if branch is skipped.

  2. 2
    Check the elif condition

    The value 18 is compared with 15. The condition 18 >= 15 is true, so the elif branch is selected.

  3. 3
    Stop checking alternatives

    The selected branch produces its message. The else branch is not selected because an earlier condition matched.

Result

The program produces the message Mild.

python
Output
Mild

The important tracing habit is to evaluate one condition at a time and follow only the path that matches. Do not read every indented print statement as if it will run. Once a branch is selected, the other alternatives in that chain are not selected for the same decision.

Writing the Structure

python

The colon marks the start of each branch's indented block. The statements belonging to a branch must be indented consistently. The conditions are arranged as an ordered chain: the initial if condition is considered first, an elif condition provides another alternative, and else represents the remaining alternative.

Mistakes That Change the Path

  • Common mistake

    Leaving out the colon after a condition or after else

    What it looks like
    if score >= 50
        print("Pass")
    Why it fails

    The branch syntax requires a colon before the indented block begins.

    Do this instead

    Write if score >= 50: before the indented statement.

  • Common mistake

    Using inconsistent indentation inside a branch

    What it looks like
    if ready:
        print("Start")
          print("Go")
    Why it fails

    Statements in the same block must be indented consistently.

    Do this instead

    Align statements that belong to the same branch.

  • Common mistake

    Putting a broader condition before a more specific condition

    What it looks like
    if score >= 0:
        print("Nonnegative")
    elif score >= 50:
        print("Pass")
    Why it fails

    For a score of 70, the first condition already matches, so the later alternative is not selected.

    Do this instead

    Order conditions so that the intended earlier cases are tested before broader cases.

  • Common mistake

    Tracing every branch instead of evaluating the condition

    What it looks like

    Assuming that both the if message and the else message will print

    Why it fails

    An if-else statement provides mutually exclusive branches, so exactly one branch executes.

    Do this instead

    Evaluate the condition and follow only the matching branch.

Practice the Trace

What do you think happens?

What message does this chain produce when points is 42?

  • Excellent

  • Pass

  • Try again

Reveal answer
Answer:

Pass

The first condition, points >= 80, is false. The elif condition, points >= 40, is true, so its branch is selected and the else branch is skipped.

python
MEDIUM

Create an if-elif-else chain for a variable named balance. Print Positive when balance is greater than 0, Zero when balance equals 0, and Negative for the remaining case. Before running it, trace the chain for balance = 0 and predict which message appears.

Hints
  • Place the test for balance greater than 0 in the if branch.
  • Place the equality test in the elif branch.
  • Use else for the remaining case.
  • Check the colon and indentation on every branch.

Key Takeaways

  1. 1
    An if-else statement creates two mutually exclusive branches for true and false outcomes.
  2. 2
    An elif chain organizes additional alternative conditions in a written order.
  3. 3
    Trace a chain by evaluating the conditions one at a time and following the selected branch.
  4. 4
    Use a colon after branch conditions and else, and indent each block consistently.
  5. 5
    Place conditions in an order that makes the intended branch reachable before a broader condition matches.

At a glance

  • Conditional statements direct execution into alternative branches.
  • An if-elif-else chain checks conditions in order and selects a matching path.
  • Exactly one branch of an if-else decision executes.
  • Correct colons, consistent indentation, and careful condition ordering are essential.
  • Tracing begins by evaluating the condition, then following only the selected branch.

Related concepts

More concepts starting with C