Concepts / Understanding if Statements and Boolean Conditions

Understanding if Statements and Boolean Conditions

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

  • Programming

One Decision, Several Outcomes

A decision sometimes has more than two possible outcomes. Python uses elif, short for else if, to connect several conditions within one decision structure. An if-elif-else chain begins with an if condition, can contain any number of elif conditions, and can optionally end with else. This lets one structure choose among several possible branches.

falsefalsefalseif conditionfirst testelif conditionnext testelif conditionanother testelseremaining cases
How does one chained conditional connect several possible outcomes?

Tracing the First Match

Python evaluates a chained conditional strictly from top to bottom. It checks the if condition first. If that condition is false, Python checks the next elif condition. It continues in this way until it finds a true condition or reaches else. When a condition is true, its branch executes and the entire chained conditional ends. Conditions below that branch are not evaluated.

truefalsetruefalsetruefalseif conditionevaluate firstelif conditionevaluate nextelif conditionevaluate nextmatching branchexecute and stopelseif all are false
How does control flow move from the first condition to the branch that runs?

What do you think happens?

Suppose the first condition in a chain is false and the second condition is true. Which branch runs?

  • Only the first branch
  • Only the second branch
  • The first and second branches
  • No branch
Reveal answer

Answer: Only the second branch

Python moves to the second condition after the first condition is false. Once the second condition is true, its branch executes and the rest of the chain is skipped.

Why Only One Branch Runs

An if-elif-else chain is exclusive: once one condition is true, exactly one branch executes and the remaining branches are skipped. This remains true even if more than one condition would be true. The first true condition controls the result because conditions are checked from top to bottom.

truestopfirst conditiontruefirst branchexecuteslater conditionsskipped
What happens when multiple conditions in a chain are true?
StructureCondition checkingPossible executions
if-elif chainStops after the first true conditionAt most one branch
Separate if statementsEvery condition is checked independentlyMultiple branches may execute

Classifying a value of 15

A value of 15 is tested against three conditions in this order: less than 20, less than 30, and less than 40. What happens in a chained conditional?

First test: The condition less than 20 is true for 15.

Branch selection: The branch belonging to the first condition executes.

Remaining tests: The conditions less than 30 and less than 40 are not evaluated as part of this chain.

Only the first true branch executes, even though all three comparisons would be true for 15.

Ordering Specific and General Tests

The order of conditions determines which branch can run. Because the first true condition ends the chain, a general condition placed too early can prevent a more specific condition below it from ever being reached. Place more specific conditions before more general ones when both could be true.

if falseif falsespecific conditionchecked firstgeneral conditionchecked latergeneral conditionchecked firstspecific conditionmay be skipped
How does changing the order of overlapping conditions change which branch runs?

Imagine choosing a category for a value. If one category describes a narrow case and another describes a wider case that includes it, testing the wider case first can capture the value before the narrow case is reached. Testing the narrow case first gives it priority. The same first-match rule applies regardless of how many elif clauses the chain contains.

Completing the Chain with else

The else clause handles every remaining case that did not satisfy an earlier condition. It has no condition of its own, must appear at the end of the chain, and executes only when all preceding if and elif conditions are false. The else clause is optional. Without it, a chain may execute no branch when every condition is false.

falsefalseno elseif conditionfalseelif conditionfalseelse branchexecutesend of chainwithout else
When is the else branch reached, and how does it complete the decision structure?

A chain with an else has exactly one executing branch because either a preceding condition is true or, if all are false, else executes. A chain without an else can execute zero branches when all of its conditions are false.

Practice the Trace

truefalsetruefalsetruecondition onetrue or falsecondition twoonly after falsecondition threeonly after falseselected branchfirst true result
What changes after each Boolean condition is evaluated, and how does true or false determine the next step?
MEDIUM

Trace a chain with three conditions and an else clause. First decide whether the first condition is true. If it is false, move to the second. Continue until you find the first true condition. If none is true, identify the else branch. Then explain why every later condition is skipped after the selected branch is found.

Hints
  • Read the conditions from top to bottom.
  • Stop at the first true condition.
  • Use else only after every earlier condition is false.
  • Treating an elif chain like several independent if statements.

    A chained conditional stops after the first true condition.

    Fix: Find the first true condition and treat its branch as the only branch that runs.

  • Putting a general condition before a more specific condition.

    The broad condition can become the first true condition and prevent the narrower test from being reached.

    Fix: Place the more specific condition before the more general condition.

  • Assuming else has its own condition.

    Else has no condition; it runs only when every earlier condition is false.

    Fix: Use elif for a specific condition and place else at the end for all remaining cases.

  • Assuming a branch always runs even when there is no else.

    Without else, zero branches can execute.

    Fix: Add an else clause when a remaining-case action is required.

Decision Structure Checklist

  • Use elif to connect multiple possible outcomes in one decision structure.
  • Remember that conditions are evaluated from top to bottom.
  • The first true condition selects the only branch that executes.
  • Place more specific conditions before more general conditions when they overlap.
  • Place else at the end; it runs only when all earlier conditions are false.
  • Use separate if statements only when independent branches are allowed to execute.

Key Takeaways

  • An elif chain handles more than two possible outcomes within one decision structure.
  • Python checks conditions from top to bottom and stops at the first true condition.
  • Exactly one branch executes in a chain when a condition matches, even if later conditions would also be true.
  • An else clause is optional, must be last, and handles the case where all previous conditions are false.
  • Separate if statements check independently and can execute multiple branches, unlike an if-elif-else chain.