2.2.2 The if-else Structure
The if-else structure allows a program to choose between two execution paths.
One block runs when the condition is True, and the other runs when it is False.
How if-else Works
- The
ifcondition is evaluated first - If True, the
ifblock executes - Otherwise, the
elseblock executes
This ensures that exactly one block is executed.
Common Use Cases
- Handling binary decisions
- Providing default behavior
- Ensuring a fallback action
📌 Note:
Useelsewhen there is a clear alternative outcome.