Skip to content

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 if condition is evaluated first
  • If True, the if block executes
  • Otherwise, the else block executes

This ensures that exactly one block is executed.


Common Use Cases

  • Handling binary decisions
  • Providing default behavior
  • Ensuring a fallback action

📌 Note:
Use else when there is a clear alternative outcome.