Expand your decision-making skills by handling multiple possible outcomes.
Imagine you are building a robot that sorts fruit. It knows what an apple is, but what happens if you hand it a banana, a grape, or even a rock? How does it decide what to do next?
In our last lesson, we learned that an if statement acts like a gate: if the condition is True, the code inside runs. But what if the condition is False? Without an else statement, the program simply skips the block and does nothing. The else keyword allows us to define a 'default' action. It captures every case that didn't meet the original criteria. Think of it as a safety net that says, 'If the first thing didn't happen, do this instead!'
Let's check if a user is old enough to enter a club where the age limit is . 1. We set a variable: `age = 10`. 2. We check the condition: `if age >= 13:`. 3. Since , the `if` block is skipped. 4. The `else:` block runs, printing 'Access Denied'.
Quick Check
Does an 'else' statement require its own condition (like 'else x > 5')?
Answer
No, an 'else' statement never has a condition; it automatically runs if the 'if' statement before it is False.
Life isn't always just 'Yes' or 'No.' Sometimes there are three, four, or even ten different possibilities! This is where elif (short for else if) comes in. An elif statement allows you to check a new condition only if the previous ones were False. You can have as many elif blocks as you want between your if and your else. This creates a chain of command where Python checks each possibility one by one until it finds a match.
Suppose we want to describe the weather based on the temperature in Celsius: 1. `if T > 30:` print 'It is hot!' 2. `elif T > 15:` print 'It is warm.' 3. `else:` print 'It is cold.' If , the first condition is False, but the second is True, so it prints 'It is warm.'
Quick Check
If the first 'if' condition is True, will Python still check the 'elif' conditions below it?
Answer
No, Python stops checking the chain as soon as it finds the first True condition.
When using multiple elif statements, the order is extremely important. Python reads your code from top to bottom. If a number satisfies the first condition, Python executes that block and ignores everything else in that chain. If you put a broad condition (like ) before a specific one (like ), the specific one will never be reached because is already greater than !
Let's categorize a test score : 1. `if S >= 90:` Grade is 'A'. 2. `elif S >= 80:` Grade is 'B'. 3. `elif S >= 70:` Grade is 'C'. 4. `else:` Grade is 'F'. If we put `S >= 70` at the very top, a student with a would get a 'C' because is True, and Python would stop right there!
Which keyword is used to check a second condition only if the first one was False?
Where must the 'else' block be placed in a logic chain?
You can use multiple 'elif' statements in a single logic chain.
Review Tomorrow
In 24 hours, try to explain to a friend why the order of 'elif' statements matters using the grade example ( vs ).
Practice Activity
Try this on your own: Write a program that asks for a number and tells the user if the number is 'Positive', 'Negative', or 'Zero'.