Else Statement
An else statement is used together with if.
It defines what should happen when the if condition is false.
How If–Else Works
Conceptually:
- If the condition is true → run the
ifblock - Otherwise → run the
elseblock
Only one of the two blocks will be executed.
Example (Python)
age = 16
if age >= 18:
print("You are allowed to enter.")
else:
print("Access denied.")
Explanation:
- If
age >= 18is false - The program runs the
elseblock instead
When to Use else
Use else when:
- you need a fallback action
- there are exactly two possible outcomes
- one condition is true and the other is false
Summary
elsehandles the opposite case ofif- It runs when the
ifcondition is false - Together,
ifandelsecover two paths