Write Simple if Statements
Alternative execution uses if-else to run one of two code blocks based on whether a condition is true or false.
Two Possible Paths
Many programs need to respond differently depending on a condition. Alternative execution handles this situation by providing two possible blocks of code: one block for when a condition is true and another block for when it is false. An if-else statement selects exactly one of those blocks.
A First Execution Trace
number = 7 if number % 2 == 0: result = "even" else: result = "odd" print(result)
How Python Chooses
When Python encounters an if-else statement, it evaluates the condition first. A condition is a boolean expression, so its result is either True or False. If the result is True, Python executes the indented code under if and skips the else block. If the result is False, Python skips the if block and executes the indented code under else. Exactly one block runs: never both and never neither.
Writing the Structure
The condition follows if, and a colon must follow the condition. The statements controlled by if must be indented. The else line also requires a colon, and the statements controlled by else must be indented. The two indented regions are the two alternative blocks.
Checking an Unexpected Result
Debugging an if statement begins at the condition. Compare the actual input with the condition and decide whether the result is True or False. Then check whether the program follows the corresponding branch. If the branch is correct but the result still seems wrong, inspect the statements inside that indented block.
Leaving out the colon after the condition.
The if statement requires a colon after its condition.
Fix:
Write if score >= 60:Leaving out the colon after else.
The else branch also requires a colon before its indented block.
Fix:
Write else:Failing to indent the code controlled by the condition.
The code that belongs to the if block must be indented.
Fix:
Indent the statement under if.Assuming both branches execute.
An if-else statement executes exactly one of its two blocks.
Fix:
Evaluate the condition and follow only the matching branch.
Branch Prediction Practice
What do you think happens?
Which message is printed when points has the value 42?
Reveal answer
Answer: high
The condition points >= 40 is True for 42. Python executes the if block and skips the else block.
Trace the code before running it. Write down the condition, decide whether it is True or False, identify the branch that executes, and state what the program prints.
Hints
- Start with the value of points.
- Evaluate points >= 40.
- Only one indented block executes.
Key Takeaways
- An if-else statement provides two alternative execution paths.
- Python evaluates the condition first and uses its True or False result to select a branch.
- Exactly one block executes in an if-else statement; the other block is skipped.
- A correct statement needs a colon after the if condition and after else, plus proper indentation for each block.
- To debug unexpected behavior, evaluate the condition with the actual input and trace the selected branch.