Nearby lessons
36 of 124C - Decision Making
Decision Making is one of the foundational topics in C programming. This lesson explains Why Decisions?, if-else vs switch — Comparison and The Conditional Operator as a Shortcut with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
Why Decisions?
A program must often choose between different paths. For example: if marks are above 40 → pass, else → fail. In C, decision making uses if, if-else, if-else-if, and switch.
In simple words: decision making lets the program choose its own road based on a condition. The condition is a yes/no question — true takes one road, false takes another.
if-else vs switch — Comparison
| Point | if-else | switch |
|---|---|---|
| Conditions | Any condition (>, <, ==, combinations) | Only equality with fixed constants |
| Data type | Anything | int or char only |
| Speed | Checks one by one | Jumps directly to the case (faster) |
| Best for | Ranges and complex conditions | Menu / fixed-value choices |
The Conditional Operator as a Shortcut
For a simple two-way choice, the ternary operator ? : replaces a small if-else in one line:
Program: ternary in action
Example04
📝 Key Takeaways
- if runs a block when the condition is true; else covers the false case.
- if-else-if ladder chooses the first true condition.
- Nested if = an if inside another if.
- switch jumps to a matching case; remember break and default.
- switch works with int/char, not float or string.
- Ternary ?: is a one-line if-else.
🧠 Test Your Knowledge
2 QuestionsProgress: 0 / 2