if-else Statements
The if statement checks a condition and executes indented code only if that condition is true.
Choosing a Path
Programs often need to respond differently depending on a condition. An if-else statement provides two alternative execution paths: one block runs when the condition is true, and the other block runs when the condition is false. Learning to trace those paths lets you predict what a program will do before it runs and helps you locate unexpected behavior.
First Trace
What do you think happens?
Suppose the condition evaluates to True. Which block executes: the if block, the else block, or both?
Reveal answer
Answer: The if block
Python evaluates the condition first. When it is True, Python executes the indented code under if and skips the else block. Exactly one block executes.
temperature = 25 if temperature > 20: print("Warm") else: print("Cool")
Python Structure
An if-else statement contains a condition and two indented code blocks. Write if, then the condition, followed by a colon. The statements belonging to the if branch must be indented. Write else followed by a colon, then indent the statements belonging to the else branch. The colon marks the start of each block, while indentation shows which statements belong to that block.
Alternative Execution
Alternative execution uses if-else to run one of two code blocks based on whether a condition is true or false. Python evaluates the condition first. If the result is True, it executes the if block and skips the else block. If the result is False, it skips the if block and executes the else block.
| Condition result | Block that executes | Block that is skipped |
|---|---|---|
| True | if block | else block |
| False | else block | if block |
An if-else statement selects exactly one of its two blocks.
Use if-else when both outcomes need an explicit response. The if block describes what should happen when the condition is true, and the else block describes the alternative when it is false. Because exactly one block executes, an if-else statement gives the program a complete two-way choice for that condition.
Second Trace
What do you think happens?
For number = 7, which message will execute in this even-or-odd decision?
Reveal answer
Answer: Odd
The condition for the even case is false for 7, so Python skips the if block and executes the else block. The two branches do not both execute.
number = 7 if number % 2 == 0: print("Even") else: print("Odd")
Debugging Branches
Debugging an if-else statement means tracing the program from the condition onward. First determine whether the condition is True or False. Then check that the selected block is the one you expected, that its statements are indented as part of the block, and that the other block is skipped. The point where the actual path differs from the expected path identifies where to investigate.
Leaving out the colon after the condition.
The syntax requires a colon after the condition.
Fix:
if score > 50:Leaving out the colon after else.
The else branch also requires a colon before its indented block.
Fix:
else:Failing to indent the statements in a branch.
Indentation identifies the code belonging to the if block.
Fix:
if score > 50: print("Pass")Expecting both branches to execute.
An if-else statement executes exactly one block: the if block for True or the else block for False.
Fix:
Evaluate the condition first, then follow only the corresponding branch.Tracing the branch before evaluating the condition.
Python evaluates the condition before selecting an execution path.
Fix:
Write down the condition's boolean result first, then identify the selected block.
Branch Practice
Trace this statement for value = 3. Identify the condition's truth value, name the block that executes, and state which block is skipped.
Hints
- Evaluate value > 5 before choosing a branch.
- A False condition selects the else block.
- Exactly one block executes.
Tracing value = 3
Determine the execution path for the if-else statement using value = 3.
Evaluate: The condition value > 5 is False because 3 is not greater than 5.
Select: The False result selects the else block.
Skip: The if block is skipped, so Large is not printed.
Execute: The else block executes and prints Small.
The output is Small. Exactly one branch executes.
Key Takeaways
- An if-else statement evaluates a condition before selecting an execution path.
- A True condition runs the indented if block and skips the else block.
- A False condition skips the if block and runs the indented else block.
- A colon is required after the condition and after else, and indentation identifies each block.
- To debug an unexpected result, evaluate the condition first and trace the selected branch step by step.
Key Takeaways
- An if-else statement provides two alternative execution paths.
- Python evaluates the condition first and executes exactly one branch.
- The if branch runs for True; the else branch runs for False.
- Correct colons and indentation are essential to the syntax.
- Tracing the condition and selected branch is the central debugging technique.