Concepts / Using else to Handle the Default Case

Using else to Handle the Default Case

elif stands for 'else if' and allows you to chain multiple conditions in a single decision structure.

  • 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.

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.

truefalsetruefalseStartx < yif conditionFirst branchx is less than yElif branchx is greater than yEndx > yelif conditionElse branchneither condition is true
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.

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

A Complete Chain

x = 15 if x < 10: print("less than 10") elif x < 20: print("between 10 and 20") else: print("20 or greater")

Tracing a value through three outcomes

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

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

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

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

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.

if not matchedif matched, chain stopsSpecific conditionchecked firstGeneral conditionchecked firstGeneral conditionchecked laterSpecific conditionmay be skipped
How does changing the order of elif conditions change which branch runs?

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.

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

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

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

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

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

  • Putting a general condition before a more specific condition.

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

    Fix: Place the more specific condition first.

  • Expecting else to test a condition.

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

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

  • Placing else before the end of the chain.

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

    Fix: 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. 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.