The if–else family is C#’s core decision‑making structure. It lets your program choose different paths based on conditions. In simple terms: if checks something, else if checks alternatives, and else handles everything that didn’t match.
Why we use them
We use if, else if, and else whenever our program needs to react to different situations — user input, data values, system states, or results of calculations. They allow your code to behave differently depending on what’s true at runtime.
This is essential for:
- Validation (e.g., checking if a username is empty)
- Branching logic (e.g., different actions based on menu choices)
- Error handling (e.g., fallback behaviour when nothing matches)
- Game logic (e.g., health thresholds, scoring rules)
- Business rules (e.g., discounts, access levels)