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.
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
- Python evaluates the if condition first.
- If it is False, it tries each elif in order; the first True one runs and the rest are skipped without being tested.
- If none is True, the else branch runs.
- 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.
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.
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.
Checking Whether a Number Is Positive
Trace the decision made when x has the value 4.
- 1Read the condition
The condition is x > 0. With x equal to 4, the comparison asks whether 4 is greater than 0.
- 2Evaluate the condition
The condition is true.
- 3Choose the block
Because the condition is true, Python executes the indented statement belonging to the if block.
The message x is positive is printed.
x is positive
Decision completeWhat do you think happens?
Suppose x is -2 in the same structure. What happens to the indented print statement?
Reveal 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
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.
if x > 0:
print('x is positive')- 1Python reads the header from left to right.
- 2Python evaluates x > 0.
- 3If the result is true, Python runs the indented print statement.
- 4If 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.
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 likeif x > 0Why it failsThe colon is one of the required parts of the if header.
Do this insteadif x > 0:Common mistake Failing to indent the controlled statement.
What it looks likeif x > 0: print('x is positive')Why it failsPython uses indentation to determine which statements belong to the if block.
Do this insteadif x > 0: print('x is positive')Common mistake Assuming a false condition runs the indented block anyway.
What it looks likeif age >= 18: print('Eligible')Why it failsA false condition causes Python to skip the entire indented block.
Do this insteadTrace the condition first, then decide whether the block executes.
Common mistake Using a condition that does not express the intended boundary.
What it looks likeUsing age > 18 when the requirement is age >= 18.
Why it failsThe condition age > 18 excludes a person whose age is exactly 18.
Do this insteadUse age >= 18 when 18-year-olds should be included.
Mental Trace Practice
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.
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
- 1An if statement checks a boolean condition and makes a decision based on whether that condition is true or false.
- 2Its header requires the if keyword, a condition, and a colon in that order.
- 3A true condition runs the following indented block; a false condition skips that block and continues with the next statement.
- 4Indentation defines which statements belong to the compound if block.
- 5Clear 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.