Using Switch Case in C: A Comprehensive Guide
Introduction
Switch case is a powerful feature in C programming language that allows you to execute different blocks of code based on the value of a variable. It is a versatile tool that can be used to handle different scenarios, such as conditional statements, loops, and conditional loops. In this article, we will explore the basics of switch case in C, including its syntax, types, and examples.
Syntax of Switch Case
The syntax of switch case in C is as follows:
switch (expression) {
case value1:
// code to be executed if expression is equal to value1
break;
case value2:
// code to be executed if expression is equal to value2
break;
// ...
default:
// code to be executed if expression is not equal to any value
break;
}
Types of Switch Case
There are two types of switch case in C:
- Single Case: This type of switch case is used when you want to execute a single block of code based on a single value.
- Multiple Cases: This type of switch case is used when you want to execute multiple blocks of code based on multiple values.
Types of Values
Switch case in C can accept the following types of values:
- Integer values: These are whole numbers, such as 1, 2, 3, etc.
- Floating-point values: These are decimal numbers, such as 3.14, -0.5, etc.
- Character values: These are single characters, such as ‘a’, ‘b’, ‘c’, etc.
Examples of Switch Case
Here are some examples of switch case in C:
- Single Case
#include <stdio.h>
int main() {
int x = 5;
switch (x) {
case 1:
printf("x is 1n");
break;
case 2:
printf("x is 2n");
break;
default:
printf("x is not 1 or 2n");
break;
}
return 0;
}
* **Multiple Cases**
```c
#include <stdio.h>
int main() {
int x = 5;
switch (x) {
case 1:
printf("x is 1n");
break;
case 2:
printf("x is 2n");
break;
case 3:
printf("x is 3n");
break;
default:
printf("x is not 1, 2, or 3n");
break;
}
return 0;
}
Switch Case with Default
The default case in switch case is used to execute a block of code when the expression is not equal to any of the values in the switch case.
- Default Case
#include <stdio.h>
int main() {
int x = 5;
switch (x) {
case 1:
printf("x is 1n");
break;
case 2:
printf("x is 2n");
break;
default:
printf("x is not 1 or 2n");
break;
}
return 0;
}
**Switch Case with Multiple Cases**
Switch case with multiple cases is used to execute multiple blocks of code based on multiple values.
* **Multiple Cases**
```c
#include <stdio.h>
int main() {
int x = 5;
switch (x) {
case 1:
printf("x is 1n");
break;
case 2:
printf("x is 2n");
break;
case 3:
printf("x is 3n");
break;
case 4:
printf("x is 4n");
break;
default:
printf("x is not 1, 2, 3, or 4n");
break;
}
return 0;
}
Switch Case with Default
Switch case with default is used to execute a block of code when the expression is not equal to any of the values in the switch case.
- Default Case
#include <stdio.h>
int main() {
int x = 5;
switch (x) {
case 1:
printf("x is 1n");
break;
case 2:
printf("x is 2n");
break;
default:
printf("x is not 1 or 2n");
break;
}
return 0;
}
**Switch Case with Multiple Default**
Switch case with multiple default is used to execute a block of code when the expression is not equal to any of the values in the switch case.
* **Multiple Default**
```c
#include <stdio.h>
int main() {
int x = 5;
switch (x) {
case 1:
printf("x is 1n");
break;
case 2:
printf("x is 2n");
break;
case 3:
printf("x is 3n");
break;
default:
printf("x is not 1, 2, or 3n");
break;
default:
printf("x is not 1, 2, 3, or 4n");
break;
}
return 0;
}
Switch Case with Multiple Cases and Default
Switch case with multiple cases and default is used to execute multiple blocks of code based on multiple values.
- Multiple Cases and Default
#include <stdio.h>
int main() {
int x = 5;
switch (x) {
case 1:
printf("x is 1n");
break;
case 2:
printf("x is 2n");
break;
case 3:
printf("x is 3n");
break;
case 4:
printf("x is 4n");
break;
default:
printf("x is not 1, 2, 3, or 4n");
break;
}
return 0;
}
**Switch Case with Multiple Default and Cases**
Switch case with multiple default and cases is used to execute multiple blocks of code based on multiple values.
* **Multiple Default and Cases**
```c
#include <stdio.h>
int main() {
int x = 5;
switch (x) {
case 1:
printf("x is 1n");
break;
case 2:
printf("x is 2n");
break;
case 3:
printf("x is 3n");
break;
case 4:
printf("x is 4n");
break;
default:
printf("x is not 1, 2, 3, or 4n");
break;
default:
printf("x is not 1, 2, 3, 4, or 5n");
break;
}
return 0;
}
Conclusion
Switch case is a powerful feature in C programming language that allows you to execute different blocks of code based on the value of a variable. It is a versatile tool that can be used to handle different scenarios, such as conditional statements, loops, and conditional loops. In this article, we have explored the basics of switch case in C, including its syntax, types, and examples. We have also discussed the different types of switch case, such as single case and multiple cases, and the different types of values that can be used in switch case. Finally, we have discussed the different ways to use switch case, including with default and multiple default cases.