Decision Making Statements: if-else Ladder & switch-case
Unit 2: Control Statements, Loops & Modular Functions • Problem Solving Using C
👨🏫 Professor's Mental Model: The Railway Track Switcher & Multi-Floor Elevator Console
if-else statement railway track badalne jaisa hai: agar signal green hai toh train main line par jayegi, warna loop line par (True/False). Aur switch-case ek high-speed elevator ke keypad jaisa hai: aapne seedha '5' dabaya aur elevator direct 5th floor par pahunch gayi, use 1st, 2nd, 3rd, 4th floor check karne ki zaroorat nahi padti (Direct Jump Table)! Lekin agar har case ke baad 'break' nahi lagaya, toh lift har floor par rukte hue niche tak slip ho jayegi (Fall-Through)!
Academic Lecture Notes & Solved Study Pages
Unit 2 • Core Concepts, Step-by-Step Proofs & Notebook Solutions
1. The 4 Forms of Conditional Branching (if Constructs)
Occurs when an else clause is ambiguously placed in nested if statements without curly braces. In C, the compiler matches the dangling else with the CLOSEST UNPAIRED preceding if statement. Always use explicit curly braces `{}` to eliminate ambiguity!
2. The switch-case Statement: Architecture & Rules
Syntax: switch (expression) { case constant_1: statements; break; case constant_2: statements; break; default: default_statements; }
Strict Rules of switch-case:
3. The Critical Role of break & Fall-Through Phenomenon
In C, case labels act merely as entry markers into the switch body.
| Parameter | else-if Ladder | switch-case Statement |
|---|---|---|
| Data Types Allowed | Any data type (integers, floats, relational expressions, pointers) | Only integer and character constants (no float/double/string) |
| Expression Types | Relational (<, >, <=) and Logical (&&, ||) ranges allowed | Only strict equality comparisons (==) against fixed constants |
| Execution Speed | Slower: Evaluates every condition sequentially O(N) | Faster: Compiler constructs an indexed Jump Table O(1) |
| Readability & Maintenance | Becomes cluttered and hard to read for >5 menu options | Extremely clean, structured, and ideal for menu-driven programs |
| Default Handling | Final optional else block catches unmatched cases | Optional default: label executes on no match |
Interactive Tested Code Example
#include <stdio.h>
int main(void) {
int option = 2;
double n1 = 20.0, n2 = 4.0, result;
printf("=== MENU-DRIVEN ARITHMETIC ENGINE ===\n");
printf("1. Add | 2. Subtract | 3. Multiply | 4. Divide\n");
printf("Selected Option: %d\n", option);
switch (option) {
case 1:
result = n1 + n2;
printf("Result: %.2f + %.2f = %.2f\n", n1, n2, result);
break;
case 2:
result = n1 - n2;
printf("Result: %.2f - %.2f = %.2f\n", n1, n2, result);
break;
case 3:
result = n1 * n2;
printf("Result: %.2f * %.2f = %.2f\n", n1, n2, result);
break;
case 4:
if (n2 == 0.0) {
printf("Error: Division by zero is mathematically undefined!\n");
} else {
result = n1 / n2;
printf("Result: %.2f / %.2f = %.2f\n", n1, n2, result);
}
break;
default:
printf("Error: Invalid option selected. Please choose 1-4.\n");
}
return 0;
}🎯 University Exam Scoring Blueprint
- In university exams, always highlight that float/double values are strictly prohibited in switch expressions.
- Draw flowcharts comparing the sequential diamond test of else-if vs the single multi-way jump of switch.
- Demonstrate intentional fall-through with an example (e.g. checking whether an input character is a vowel).