C switch-case, Fall-Through Behavior & 7 Decision Practice Programs
Welcome to Phase 4 (Part 2): C switch-case, Fall-Through Mechanics & 7 Decision Programs Masterclass! When a program must branch across numerous fixed constant options (such as menu choices, state machines, or arithmetic operations), writing long else-if chains becomes verbose and slow. The C switch-case construct solves this by allowing compilers to generate ultra-fast $O(1)$ Jump Tables. In this comprehensive guide, you will master the internal architecture of switch, the critical role of break, intentional fall-through grouping, and construct 7 complete production-grade practical decision algorithms.
In C, switch operates exclusively on Integral Values (int, char, enum). Floating-point numbers (float, double) and strings (char[]) are NOT allowed in C switch statements!
โ๏ธ Why switch is Faster than else-if (Jump Tables)
โข else-if Chain: Checks conditions linearly one-by-one ($O(N)$ time complexity). If matching branch is at the 10th position, 10 comparisons are executed.
โข switch-case: GCC compiler case values ni array of jump memory addresses (Jump Table / Branch Table) ga compile chesthundi. CPU directly computes target branch address in $O(1)$ Instant Time!
switch(choice) โโโบ JumpTable[choice] โโโบ Direct Jump to Case Block (No linear checks!)
In C, when a matching case is found, execution continues sequentially into subsequent cases until a break; is reached or switch block ends. This is called Fall-Through:
๐ก Deliberate Fall-Through for Grouping Cases
Multiple cases ki same execution logic unte, break omit chesi group cheyyavachu:
#include <stdio.h>
int main(void) {
char ch = 'E';
switch(ch) {
case 'A': case 'a':
case 'E': case 'e':
case 'I': case 'i':
case 'O': case 'o':
case 'U': case 'u':
printf("'%c' is a VOWEL.\n", ch);
break;
default:
printf("'%c' is a CONSONANT or non-alphabetic character.\n", ch);
break;
}
return 0;
}
Mastering conditions through 7 foundational programming algorithms:
Programs 1 & 2: Even/Odd & Positive/Negative/Zero Checker
#include <stdio.h>
void checkNumber(int n) {
// 1. Even or Odd
if (n % 2 == 0) {
printf("%d is EVEN. ", n);
} else {
printf("%d is ODD. ", n);
}
// 2. Positive, Negative, or Zero
if (n > 0) {
printf("State: POSITIVE\n");
} else if (n < 0) {
printf("State: NEGATIVE\n");
} else {
printf("State: ZERO\n");
}
}
int main(void) {
checkNumber(14);
checkNumber(-7);
checkNumber(0);
return 0;
}
Programs 3 & 4: Largest of Three Numbers & Leap Year Checker
#include <stdio.h>
int findLargest(int a, int b, int c) {
if (a >= b && a >= c) return a;
if (b >= a && b >= c) return b;
return c;
}
int isLeapYear(int year) {
// Leap year rule: Divisible by 4 AND not 100, UNLESS divisible by 400!
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main(void) {
printf("Largest of (45, 92, 78): %d\n", findLargest(45, 92, 78));
printf("Is 2024 a Leap Year? %s\n", isLeapYear(2024) ? "YES" : "NO");
printf("Is 1900 a Leap Year? %s\n", isLeapYear(1900) ? "YES" : "NO (Century exception)");
printf("Is 2000 a Leap Year? %s\n", isLeapYear(2000) ? "YES (400 rule)" : "NO");
return 0;
}
Programs 5, 6 & 7: Menu Calculator using switch & Voting Eligibility
#include <stdio.h>
void calculate(double a, double b, char op) {
switch(op) {
case '+':
printf("%.2f + %.2f = %.2f\n", a, b, a + b);
break;
case '-':
printf("%.2f - %.2f = %.2f\n", a, b, a - b);
break;
case '*':
printf("%.2f * %.2f = %.2f\n", a, b, a * b);
break;
case '/':
if (b == 0.0) {
printf("โ Error: Division by zero is undefined!\n");
} else {
printf("%.2f / %.2f = %.2f\n", a, b, a / b);
}
break;
default:
printf("โ Unknown Operator '%c'\n", op);
break;
}
}
int main(void) {
printf("--- Multi-Operation Switch Calculator ---\n");
calculate(120.0, 30.0, '+');
calculate(120.0, 30.0, '/');
calculate(50.0, 0.0, '/');
// Voting Eligibility Check
int age = 17;
printf("\nAge %d Voting Status: %s\n", age, (age >= 18) ? "Eligible" : "Underage");
return 0;
}
Run this multi-operator switch calculator in our online GCC compiler:
#include <stdio.h>
int main(void) {
int year = 2028;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("Year %d is a LEAP YEAR (366 days).\n", year);
} else {
printf("Year %d is a COMMON YEAR (365 days).\n", year);
}
return 0;
}