Module 5 · Conditional Statements — topic 3 of 10
if-else
if-else chooses between exactly two actions. Learn its syntax, how to choose which case goes first, and when an if-else can be replaced by returning a Boolean directly.
Learning objectives
- Write if-else statements that choose between two actions
- Order the branches for readability
- Avoid redundant if-else around Booleans
Definition
if-else chooses between exactly two actions. Learn its syntax, how to choose which case goes first, and when an if-else can be replaced by returning a Boolean directly.
Introduction
if-else describes an either/or choice. If the condition is true the first block runs; otherwise the else block runs. Exactly one of the two always runs, never both and never neither.
Why it matters
Many decisions have exactly two outcomes: pass or fail, valid or invalid, in stock or out of stock. Writing them as if-else makes it obvious to a reader that every situation is covered.
Detailed explanation
else has no condition of its own; it catches every case where the if condition is false. It is followed by a colon and its own indented block, aligned with the if.
Because the branches are mutually exclusive and together exhaustive, if-else guarantees that something happens. This is useful for producing a value that must always be set, such as a status message.
Order the branches so the code reads naturally. A common style is to put the normal or expected case first and the exceptional case in else, or to put the shorter branch first so the reader sees both quickly.
If both branches only produce a value, the conditional expression on one line (covered later in this module) may be clearer.
When the branches merely return True or False, the comparison itself is already a Boolean: return age >= 18 replaces a four-line if-else.
Syntax
- else lines up with if.
- else takes no condition.
- Exactly one of the two blocks runs.
Code examples
Pass or fail
Pass
Even or odd
13 is odd
More examples
Stock check and a simplified Boolean
Sold 5 pen, 7 left Cannot sell 1 ink Cannot sell 2 paper
Step-by-step explanation
- can_buy returns the comparison directly instead of an if-else returning True or False.
- stock.get(item, 0) treats unknown items such as paper as having zero stock.
- The if branch reduces stock and reports the sale; the else branch reports a refusal.
- Each request goes through exactly one of the two branches.
Expected output
Running “Stock check and a simplified Boolean” prints exactly:
Practical applications
- Login pages show the dashboard if credentials are valid, otherwise an error.
- Payment systems either approve or decline a transaction.
- Smart lights turn on if motion is detected, otherwise stay off.
Best practices
- Use if-else when there are exactly two outcomes.
- Keep both branches short; move long logic into functions.
- Return comparisons directly instead of wrapping them in if-else.
Common mistakes
Giving else a condition
Avoid
Prefer
else never takes a condition. Use elif if you need another test.
Verbose Boolean returns
Avoid
Prefer
The comparison already produces True or False.
Key takeaways
- if-else runs exactly one of two blocks.
- else has no condition and catches everything else.
- A Boolean expression can often replace an if-else that returns True/False.
Try it yourself
- 1Ask for a number and print whether it is positive or not positive using if-else.
- 2Rewrite this as one line: if x > 0: result = True else: result = False.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?