Branching Structures
Beyond a simple `if`, we use `else` for alternatives and `elif` for checking multiple conditions. Click the parts to learn their roles.
if / else
if / elif / else
Identify all parts in both examples!
Predict the Path
Only one block (`if`, `elif`, or `else`) will run. Given the input value, predict which message will be printed by the code below.
Code Challenge: Temp Check
Write code using `if`, `elif`, and `else` to handle multiple temperature ranges.
- "Hot" if temperature is greater than 30.
- "Warm" if temperature is greater than or equal to 20 (but not > 30).
- "Cold" otherwise (if less than 20).
Computational Thinking Enhanced
How do `else` and `elif` expand our computational problem-solving?
Introducing `else` and `elif` significantly enhances Algorithmic Thinking by allowing for more complex decision structures.
- `else` provides a default action, ensuring the algorithm always does *something* even if the initial `if` condition isn't met. This mirrors real-world "otherwise" scenarios.
- `elif` enables checking multiple, mutually exclusive conditions sequentially. This is crucial for tasks like categorization (e.g., grading, temperature ranges) where only one outcome should apply.
Advanced Practice Complete!
Fantastic! You've mastered `else` and `elif` for more complex decision-making.
Consolidate your understanding by watching the Video Lesson for Module 6.