1switch (n)
2{
3 case 1: // code to be executed if n = 1;
4 break;
5 case 2: // code to be executed if n = 2;
6 break;
7 default: // code to be executed if n doesn't match any cases
8}
1#include <stdio.h>
2
3int main(void)
4{
5 int a = 0;
6
7 switch(a)
8 {
9 case 1 :
10 statement("a = 1");
11 break;
12
13 case 2 :
14 printf("a = 2");
15 break;
16
17 default :
18 printf("a is neither 1 or 2");
19 break;
20 }
21}
1switch (expression) {
2 case constant1:
3 // statements
4 break;
5
6 case constant2:
7 // statements
8 break;
9
10 default:
11 // default statements
12}
1int i;
2
3switch (i){
4 case 1:
5 //The Proccess you want to be executed
6 break;
7 case 2:
8 //The Proccess you want to be executed
9 break;
10 default:
11 break;
12}
1// example of switch case statement
2#include <stdio.h>
3
4int main() {
5 char operator;
6 double n1, n2;
7
8 printf("Enter an operator (+, -, *, /): ");
9 scanf("%c", &operator);
10 printf("Enter two operands: ");
11 scanf("%lf %lf",&n1, &n2);
12
13 switch(operator)
14 {
15 case '+':
16 printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);
17 break;
18
19 case '-':
20 printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);
21 break;
22
23 case '*':
24 printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);
25 break;
26
27 case '/':
28 printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);
29 break;
30
31 // operator doesn't match any case constant +, -, *, /
32 default:
33 printf("Error! operator is not correct");
34 }
35
36 return 0;
37}
38
1Switch(integer-expression)
2
3{
4
5Case c1:
6
7 Code;
8
9Case c2: //c1,c2,c3 are constants
10
11 Code; //Code is any valid C code
12
13Case c3:
14
15 Code;
16
17default:
18
19 Code;
20
21}