Switch in c
n C, there is a control flow statement known as the switch statement that offers an effective method of handling numerous situations of a single expression. It allows you to compare the value of an expression to a list of possible constant values and execute code blocks based on the match.
Here is the fundamental syntax of the switch statement in C:
switch (expression) {
case value1:
// If expression equals value1, run this code.
break;
case value2:
//If expression equals value2, run this code.
break;
// ... more cases ...
default:
//If none of the cases match, run the following code.
}
example:-
#include <stdio.h>
#include<conio.h>
int main() {
int n = 3;
switch (n) {
case 1:
printf("print case 1\n");
break;
case 2:
printf("print case 2\n");
break;
case 3:
printf("print case 3\n");
break;
case 4:
printf("print case 4\n");
break;
default:
printf("case default\n");
}
getch();
}