Concepts / Using else and elif for Multiple Conditions

Using else and elif for Multiple Conditions

An if statement checks a boolean condition and executes an indented block only if that condition is true.

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

The Decision Path

Programs become useful when they can respond to different situations instead of running exactly the same code every time. An if statement creates a decision point: Python checks a condition, runs an indented block when that condition is true, and skips that block when the condition is false. This is the foundation for tracing programs that later need to distinguish among multiple conditions.

if statementconditiontrue or falseindented blockexecutesnext statementwhen block is skippedevaluatetruefalse
What happens after Python evaluates the condition?

Tracing a Condition

When Python encounters an if statement, it first evaluates the condition. A condition is a boolean expression, so its result is either true or false. A true result sends execution into the indented block immediately following the colon. A false result causes Python to skip the entire indented block and continue with the next statement after that block.

Worked example

Checking Whether a Number Is Positive

Trace the decision made when x has the value 4.

  1. 1
    Read the condition

    The condition is x > 0. With x equal to 4, the comparison asks whether 4 is greater than 0.

  2. 2
    Evaluate the condition

    The condition is true.

  3. 3
    Choose the block

    Because the condition is true, Python executes the indented statement belonging to the if block.

Result

The message x is positive is printed.

python
Output
x is positive
Decision complete

What do you think happens?

Suppose x is -2 in the same structure. What happens to the indented print statement?

  • It runs because x exists

  • It is skipped because x > 0 is false

  • Python changes x to 0

  • Both the indented statement and the next statement are skipped

Reveal answer
Answer:

It is skipped because x > 0 is false.

Python evaluates the condition first. When the result is false, it skips the entire indented block and continues with the next statement after the if block.

Reading the Header

Definition

An if statement is a compound statement. Its header contains three essential parts in order: the if keyword, a condition that evaluates to true or false, and a colon. The statements controlled by that header follow on indented lines.

ifkeywordprint(...)indented blockx > 0boolean condition:header endfollowed by
Which parts form the header, and which statements belong to its indented block?
Code walkthrough
if x > 0:
    print('x is positive')
  1. 1
    Python reads the header from left to right.
  2. 2
    Python evaluates x > 0.
  3. 3
    If the result is true, Python runs the indented print statement.
  4. 4
    If the result is false, Python skips that statement.
  • if is the keyword.
  • x > 0 is the condition.
  • : marks the end of the header.
  • The indented print statement belongs to the if block.

Indentation as Grouping

Indentation is part of Python syntax, not merely a formatting preference. Statements indented under the header belong to the if block. Multiple statements with the same indentation belong to that block together. A statement aligned with the if keyword is outside the block, so it runs after the if statement regardless of whether the condition was true or false.

if condition:headerstatement oneindentedstatement twosame indentationnext statementaligned with ifcontainscontainsblock ends before
Which statements belong to the if header, and where does the compound block end?
python

In this example, the two indented print statements are grouped together. If temperature > 20 is true, both run. If it is false, both are skipped. The final print statement is aligned with if, so it is outside the block and runs after the if statement in either case.

Mistakes to Avoid

  • Common mistake

    Leaving out the colon after the condition.

    What it looks like
    if x > 0
    Why it fails

    The colon is one of the required parts of the if header.

    Do this instead
    if x > 0:
  • Common mistake

    Failing to indent the controlled statement.

    What it looks like
    if x > 0:
    print('x is positive')
    Why it fails

    Python uses indentation to determine which statements belong to the if block.

    Do this instead
    if x > 0:
        print('x is positive')
  • Common mistake

    Assuming a false condition runs the indented block anyway.

    What it looks like
    if age >= 18:
        print('Eligible')
    Why it fails

    A false condition causes Python to skip the entire indented block.

    Do this instead

    Trace the condition first, then decide whether the block executes.

  • Common mistake

    Using a condition that does not express the intended boundary.

    What it looks like

    Using age > 18 when the requirement is age >= 18.

    Why it fails

    The condition age > 18 excludes a person whose age is exactly 18.

    Do this instead

    Use age >= 18 when 18-year-olds should be included.

Mental Trace Practice

EASY

For each case, identify the condition, decide whether it is true or false, and state whether the indented block runs: first consider x = 7 for x > 0, then consider x = -1 for x > 0.

Hints
  • Evaluate the comparison using the stated value of x.
  • A true condition runs the indented block.
  • A false condition skips the indented block.
Best practice

Before running an if statement, trace it line by line. Ask what the condition is, whether it is true or false for the current values, and which indented statements belong to the block. Keep the condition clear and directly connected to the decision you want the program to make.

Key Takeaways

  1. 1
    An if statement checks a boolean condition and makes a decision based on whether that condition is true or false.
  2. 2
    Its header requires the if keyword, a condition, and a colon in that order.
  3. 3
    A true condition runs the following indented block; a false condition skips that block and continues with the next statement.
  4. 4
    Indentation defines which statements belong to the compound if block.
  5. 5
    Clear condition tracing is the starting point for reasoning about more than one possible program path.

At a glance

  • An if statement evaluates a boolean condition before deciding what to execute.
  • The required header parts are the if keyword, the condition, and the colon.
  • Indentation groups one or more statements into the if block.
  • When the condition is false, Python skips the entire indented block and continues after it.
  • Careful mental tracing provides the foundation for understanding multiple decision paths.

Related concepts

More concepts starting with U