C language notes
3 min readAug 19, 2025
What is a Switch Statement?
switchis a selection statement in C.- It allows us to execute only one case from many cases depending on the value of an expression.
- It works like a ladder if-else, but internally it is faster because it jumps directly to the matching case.
🔹 Rules of Switch
- Case values must be constant integer or char (no float, no string, no variables).
- No duplicate cases allowed.
- Case must end with
:(colon). - Break is used to stop execution after one case. If not, execution will fall through to the next case.
- Default works like
else(optional). - Case values cannot be ranges (like
1..10not allowed).
Keywords: switch, case, break, default.
🔹 Syntax
switch(expression) {
case constant1:
// statements
break;
case constant2:
// statements
break;
...
default:
// statements
}🔹 5 Good Examples of Switch in C
1. Check Even / Odd
#include <stdio.h>
int main() {
int n;
scanf("%d", &n); switch(n % 2) {
case 0:
printf("%d is Even\n", n);
break;
case 1:
printf("%d is Odd\n", n);
break;
}
return 0;
}2. Simple Calculator
#include <stdio.h>
int main() {
int a, b;
char op;
scanf("%d %d %c", &a, &b, &op); switch(op) {
case '+': printf("Sum = %d\n", a + b); break;
case '-': printf("Difference = %d\n", a - b); break;
case '*': printf("Product = %d\n", a * b); break;
case '/': printf("Quotient = %d\n", a / b); break;
default: printf("Invalid Operator\n");
}
return 0;
}3. Day of the Week
#include <stdio.h>
int main() {
int day;
scanf("%d", &day); switch(day) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
case 4: printf("Thursday\n"); break;
case 5: printf("Friday\n"); break;
case 6: printf("Saturday\n"); break;
case 7: printf("Sunday\n"); break;
default: printf("Invalid Day\n");
}
return 0;
}Get Aman Verma’s stories in your inbox
Join Medium for free to get updates from this writer.
4. Grade Evaluation
#include <stdio.h>
int main() {
int marks;
scanf("%d", &marks); switch(marks / 10) { // divide to get grade range
case 10:
case 9: printf("Grade: A\n"); break;
case 8: printf("Grade: B\n"); break;
case 7: printf("Grade: C\n"); break;
case 6: printf("Grade: D\n"); break;
default: printf("Grade: F\n");
}
return 0;
}5. Menu-Driven Program
#include <stdio.h>
int main() {
int choice;
printf("1. Hello\n2. Bye\n3. Exit\n");
scanf("%d", &choice); switch(choice) {
case 1: printf("Hello User!\n"); break;
case 2: printf("Goodbye!\n"); break;
case 3: printf("Exiting...\n"); break;
default: printf("Invalid Choice\n");
}
return 0;
}✅ These are common real-life uses of switch:
- Mathematical calculations
- Menus in programs
- Checking conditions (like even/odd, days, grades)
second day — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
🔹 Corrected & Modern Version (works in GCC/VS Code)
#include <stdio.h>
#include <stdlib.h>int main() {
int qty, op, amt = 0; while (1) {
printf("\n**********************************************\n");
printf("\t\t\t HOTEL SAI BALAJI\n");
printf("\t\t\t AMEERPET - HYD\n");
printf("**********************************************\n");
printf("\t1. Tea - 10/-\n");
printf("\t2. Coffee / Milk / Boost / Lemon Tea / Water Bottle - 20/-\n");
printf("\t3. Idly / Bonda / Plain dosa / Upma / Poha - 30/-\n");
printf("\t4. Sambar idly / Wada / Onion / Masala dosa / Poori - 40/-\n");
printf("\t5. Sambar wada / Veg Biryani / Paneer dosa - 50/-\n");
printf("\t6. Bill\n");
printf("\t7. Cancel\n");
printf("\t8. Close\n");
printf("**********************************************\n"); printf("\tEnter your option: ");
scanf("%d", &op); if (op >= 1 && op <= 5) {
printf("Enter Quantity: ");
scanf("%d", &qty);
} switch (op) {
case 1: amt += qty * 10; break;
case 2: amt += qty * 20; break;
case 3: amt += qty * 30; break;
case 4: amt += qty * 40; break;
case 5: amt += qty * 50; break; case 6:
printf("\nYour total billed amount = Rs. %d\n", amt);
amt = 0; // reset after billing
break; case 7:
printf("\nYour order is cancelled.\n");
amt = 0;
break; case 8:
printf("\nApplication Terminated. Thank you!\n");
exit(0); default:
printf("\nInvalid Option! Try again.\n");
}
} return 0;
}
🔹 Sample Run
************ HOTEL SAI BALAJI ************
1. Tea - 10/-
2. Coffee ... - 20/-
3. Idly ... - 30/-
...
Enter your option: 1
Enter Quantity: 2Enter your option: 3
Enter Quantity: 1Enter your option: 6
Your total billed amount = Rs. 50







