Nearby lessons

36 of 124

C - 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

Pointif-elseswitch
ConditionsAny condition (>, <, ==, combinations)Only equality with fixed constants
Data typeAnythingint or char only
SpeedChecks one by oneJumps directly to the case (faster)
Best forRanges and complex conditionsMenu / 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
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int marks = 60;
6 char result;
7 
8 result = (marks >= 40) ? 'P' : 'F'; // same as if-else
9 printf("Result: %c\n", result); // P
10}
Output
Result: P
📝 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 Questions
Progress: 0 / 2