Concepts / Using else to Handle the Default Case

Using else to Handle the Default Case

An if-elif-else chain is one decision structure. It is different from writing several independent if statements.

  • Programming

One Decision, Several Outcomes

A decision does not always have only two possible outcomes. Sometimes a program must choose among several results. Python uses elif, short for else if, to connect multiple conditions in one decision structure. An elif chain checks its conditions in order and selects the first branch whose condition is true.

Key point

An if-elif-else chain is one decision structure. It is different from writing several independent if statements.

Following the Chain

Python evaluates a chained conditional from top to bottom. It checks the if condition first. If that condition is false, Python checks the next elif condition. This continues until a true condition is found. As soon as one condition is true, its branch runs and the rest of the chain is skipped.

Startx < yif conditionFirst branchx is less than yElif branchx is greater than yEndx > yelif conditionElse branchneither condition is truetruefalsetruefalse
How does control flow move through each condition, and which branch runs when a condition is met?

The First True Branch

Exactly one branch executes in an if-elif-else chain. This remains true even when more than one condition would be true. Python does not continue checking after it finds the first true condition. The later conditions are skipped, so the chain produces one selected outcome rather than several.

x = 15x < 20trueFirst branchselectedEndx < 30not checkedx < 40not checkedtruestop checking
What happens when multiple conditions are true, and why does only the first matching branch execute?

A Complete Chain

Code walkthrough
x = 15

if x < 10:
    print("less than 10")
elif x < 20:
    print("between 10 and 20")
else:
    print("20 or greater")
  1. 1
    Python begins with the if condition.
  2. 2
    Because 15 is not less than 10, Python moves to the elif condition.
  3. 3
    Because 15 is less than 20, Python prints the second message.
  4. 4
    The chain ends after that branch; Python does not evaluate the else branch.
  • The if condition x < 10 is false.
  • Python checks the elif condition x < 20.
  • The elif condition is true, so its branch executes.
  • The else branch is skipped because a previous condition was true.
Output
between 10 and 20
Worked example

Tracing a value through three outcomes

Determine which message is printed when x has the value 15.

  1. 1
    Check if

    The condition x < 10 is false, so its branch does not run.

  2. 2
    Check elif

    The condition x < 20 is true, so the elif branch runs.

  3. 3
    Skip else

    The else branch is not considered because a previous condition was true.

Result

The program prints between 10 and 20.

Ordering the Conditions

Because conditions are checked from top to bottom, their order affects the result. Put a more specific condition before a more general condition. If a general condition appears first and is true, Python stops there and never reaches a later condition that might describe the case more precisely.

Specific conditionchecked firstGeneral conditionchecked firstGeneral conditionchecked laterSpecific conditionmay be skippedif not matchedif matched, chain stops
How does changing the order of elif conditions change which branch runs?
Before
if x < 20:
    print("less than 20")
elif x < 10:
    print("less than 10")

The general condition x < 20 appears first, so a value less than 10 matches it before the specific condition is reached.

After
if x < 10:
    print("less than 10")
elif x < 20:
    print("less than 20")

The specific condition x < 10 appears first, so values less than 10 can receive the more specific result.

The Default Branch

The else clause represents the remaining case. It has no condition of its own. Python reaches it only when the if condition and every elif condition before it are false. If an else clause is included, one branch of the chain will execute. Without else, it is possible for no branch to run.

if conditionfalseelif conditionfalseelsedefault caseEndfalsefalse
What happens when none of the if or elif conditions is true, and how does else handle that remaining case?
Best practice

Use elif for a specific additional condition. Use else when the program should handle every case that did not match an earlier condition. Keep else at the end of the chain.

Mistakes Beginners Make

  • Common mistake

    Writing several independent if statements when only one outcome should be selected.

    What it looks like
    if x < 20: ...
    if x < 30: ...
    if x < 40: ...
    Why it fails

    A value such as 15 satisfies all three conditions, so all three branches can execute.

    Do this instead

    Use an if-elif chain when the branches represent one choice and only one branch should run.

  • Common mistake

    Putting a general condition before a more specific condition.

    What it looks like
    if x < 20: ...
    elif x < 10: ...
    Why it fails

    A value less than 10 also satisfies x < 20, so the chain stops before the specific condition is checked.

    Do this instead

    Place the more specific condition first.

  • Common mistake

    Expecting else to test a condition.

    What it looks like
    else x == 20:
    Why it fails

    Else has no condition. It handles the remaining cases after all earlier conditions are false.

    Do this instead

    Use elif when another specific condition must be tested, and reserve else for the final default case.

  • Common mistake

    Placing else before the end of the chain.

    What it looks like
    if condition_a: ...
    else: ...
    elif condition_b: ...
    Why it fails

    An else clause must come at the end of the if-elif-else structure.

    Do this instead

    Put every elif before the final else.

Practice the Trace

EASY

Trace the chain from top to bottom. Which message is printed when x is 25? How many conditions are checked before a branch runs?

if x < 10:
    print("less than 10")
elif x < 20:
    print("between 10 and 20")
elif x < 30:
    print("between 20 and 30")
else:
    print("30 or greater")
Hints
  • Check the if condition first.
  • Continue to the next elif only when the earlier condition is false.
  • Stop as soon as you find the first true condition.

What do you think happens?

What happens when choice is a value other than a, b, or c in a chain with three matching if or elif conditions and no else clause?

Reveal answer
Answer:

None of the branches executes.

Each condition is false for that value, and without an else clause there is no remaining branch to run.

Working Rules

  1. 1
    Use elif to connect multiple possible outcomes in one decision structure. Python checks the conditions from top to bottom and executes only the first true branch. Put more specific conditions before general ones so the intended case is reached first. Place else at the end when every unmatched case should receive a default action. If there is no else and every condition is false, no branch runs.

Key Takeaways

  • An elif chain handles more than two possible outcomes in one decision structure.
  • Conditions are evaluated from top to bottom.
  • Only the first true branch executes, even if later conditions are also true.
  • More specific conditions should come before general conditions.
  • An optional else clause must come last and handles the remaining cases.

Related concepts

More concepts starting with U