Write if-elif-else Statements
Alternative execution uses if-else to run one of two code blocks based on whether a condition is true or false.
One Decision, Two Paths
Programs often need to choose between two actions. Alternative execution handles this choice: Python evaluates a condition, then runs one code block when the condition is true and another code block when the condition is false. The supplied material establishes this two-branch if-else form as the foundation for understanding alternative execution.
Following the Execution Path
Tracing means following the decision in order. First, evaluate the condition. If it produces True, follow the if path and execute the indented code under if. Python skips the else block. If the condition produces False, skip the if block and execute the indented code under else. Exactly one of these two blocks runs: never both and never neither.
What do you think happens?
Suppose a condition evaluates to False. Which branch executes?
Reveal answer
Answer: The else block
Python skips the if block when its condition is False and executes the code under else.
Writing the Branch Structure
The structure has four essential parts. Write if, followed by a condition and a colon. Put the first block on the indented lines below it. Then write else followed by a colon, and put the second block on the indented lines below else. The condition is a boolean expression, so it results in either True or False.
Even and Odd Trace
The source material identifies an even-or-odd example as a useful way to trace alternative execution. The following concrete Python version is a generated teaching example that applies the documented if-else rules.
number = 8 if number % 2 == 0: result = "even" else: result = "odd" print(result)
evenMistakes in Branching
Leaving out the colon after the condition.
The documented syntax requires a colon after the condition.
Fix:
Write if condition: before the indented first block.Leaving out the colon after else.
The documented syntax requires a colon after else.
Fix:
Write else: before the indented second block.Treating the two blocks as if both will execute.
Python executes exactly one block: the if block when the condition is true, or the else block when it is false.
Fix:
Evaluate the condition first, then follow only the matching branch.Ignoring indentation when identifying a block.
The source requires proper indentation for each block.
Fix:
Place each block's code on indented lines beneath its if or else header.
When tracing any documented if-else statement, use a fixed order: identify the condition, decide whether it is True or False, select the matching block, and mark the other block as skipped. This prevents the common mistake of mentally executing both alternatives.
Branch Selection Practice
Trace the following statement for a value of 3. Which value is assigned to result?
Hints
- Evaluate the condition before choosing a block.
- Decide whether 3 is greater than 5.
- Only one of the two indented blocks executes.
Tracing value 3
Determine which branch runs when value is 3.
Evaluate the condition: The condition value > 5 is false because 3 is not greater than 5.
Choose the path: Because the condition is false, Python skips the if block.
Run the alternative: Python executes the indented else block and assigns not large to result.
The else block executes, and result receives not large.
Key Takeaways
- Alternative execution chooses between two code blocks according to whether a condition is True or False.
- An if-else statement requires a colon after the condition and after else.
- Each branch is written as a properly indented block.
- Python evaluates the condition first, then executes exactly one branch.
- To trace a statement, determine the condition's result and follow only the corresponding execution path.
Key Takeaways
- Use alternative execution when one of two code blocks should run.
- Write the condition and if header with a colon, then indent the first block.
- Write else followed by a colon, then indent the alternative block.
- Evaluate the condition before tracing the program's path.
- Exactly one block executes in the documented if-else form.