switch-case statements in C Language:
Switch-Case in C
The switch-case
statement in C is a control statement that allows a variable to be tested against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
Basic Switch-Case
The basic syntax of the switch-case
statement is as follows:
#include
int main() {
int number = 2;
switch (number) {
case 1:
printf("Number is 1\n");
break;
case 2:
printf("Number is 2\n");
break;
case 3:
printf("Number is 3\n");
break;
default:
printf("Number is not 1, 2, or 3\n");
break;
}
return 0;
}
Handling Different Data Types
The switch-case
statement can handle char
, int
, and enum
types. Here's an example with each type:
Char Type
#include
int main() {
char grade = 'B';
switch (grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
case 'C':
printf("Well done\n");
break;
case 'D':
printf("You passed\n");
break;
case 'F':
printf("Better try again\n");
break;
default:
printf("Invalid grade\n");
break;
}
return 0;
}
Enum Type
#include
enum day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
int main() {
enum day today = Wednesday;
switch (today) {
case Monday:
printf("Today is Monday\n");
break;
case Tuesday:
printf("Today is Tuesday\n");
break;
case Wednesday:
printf("Today is Wednesday\n");
break;
case Thursday:
printf("Today is Thursday\n");
break;
case Friday:
printf("Today is Friday\n");
break;
case Saturday:
printf("Today is Saturday\n");
break;
case Sunday:
printf("Today is Sunday\n");
break;
default:
printf("Invalid day\n");
break;
}
return 0;
}
Nesting Switch Cases
Nesting switch
statements means having a switch
statement inside another switch
statement. Here's an example:
#include
int main() {
int num1 = 1, num2 = 2;
switch (num1) {
case 1:
printf("num1 is 1\n");
switch (num2) {
case 2:
printf("num2 is 2\n");
break;
default:
printf("num2 is not 2\n");
break;
}
break;
default:
printf("num1 is not 1\n");
break;
}
return 0;
}
Examples
Here are some additional examples demonstrating the usage of switch-case
in C:
#include
int main() {
int number = 3;
switch (number) {
case 1:
printf("Number is 1\n");
break;
case 2:
printf("Number is 2\n");
break;
case 3:
printf("Number is 3\n");
break;
default:
printf("Number is not 1, 2, or 3\n");
break;
}
char grade = 'A';
switch (grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Well done\n");
break;
case 'C':
printf("Good\n");
break;
case 'D':
printf("You passed\n");
break;
case 'F':
printf("Better try again\n");
break;
default:
printf("Invalid grade\n");
break;
}
enum day {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
enum day today = Fri;
switch (today) {
case Mon:
printf("Today is Monday\n");
break;
case Tue:
printf("Today is Tuesday\n");
break;
case Wed:
printf("Today is Wednesday\n");
break;
case Thu:
printf("Today is Thursday\n");
break;
case Fri:
printf("Today is Friday\n");
break;
case Sat:
printf("Today is Saturday\n");
break;
case Sun:
printf("Today is Sunday\n");
break;
default:
printf("Invalid day\n");
break;
}
return 0;
}