Before start, please go through what is switch statement in C programming?
Switch is a control statement that allows a value to change control over execution.
Below is the example of switch statement:
// Following is a simple program to demonstrate syntax of switch. #include <stdio.h> int main() { int x = 2; switch (x) { case 1: printf("Choice is 1"); break; case 2: printf("Choice is 2"); break; case 3: printf("Choice is 3"); break; default: printf("Choice other than 1, 2 and 3"); break; } return 0; }
Output:
Choice is 2
The following are some important information about Switch Statement.
1) The expression used in the switch must be integral type ( int, char and enum).
So any other type of expression are not allowed.
// float is not allowed in switch #include <stdio.h> int main() { float x = 1.1; switch (x) { case 1.1: printf("Choice is 1"); break; default: printf("Choice other than 1, 2 and 3"); break; } return 0; }
Output:
Compiler Error: switch quantity not an integer
2) All the statements following a matching case execute until a break statement is reached.
It Means, until a break statement is reach the expression match with case labels.
// There is no break in all cases #include <stdio.h> int main() { int x = 2; switch (x) { case 1: printf("Choice is 1\n"); case 2: printf("Choice is 2\n"); case 3: printf("Choice is 3\n"); default: printf("Choice other than 1, 2 and 3\n"); } return 0; }
Output:
Choice is 2
Choice is 3
Choice other than 1, 2 and 3
// There is no break in some cases #include <stdio.h> int main() { int x = 2; switch (x) { case 1: printf("Choice is 1\n"); case 2: printf("Choice is 2\n"); case 3: printf("Choice is 3\n"); case 4: printf("Choice is 4\n"); break; default: printf("Choice other than 1, 2, 3 and 4\n"); break; } printf("After Switch"); return 0; }
Output:
Choice is 2
Choice is 3
Choice is 4
After Switch
3) The default block can be placed anywhere.
It means, the position of default doesn’t matter it is still executed when no match found.
// The default block is placed above other cases. #include <stdio.h> int main() { int x = 4; switch (x) { default: printf("Choice other than 1 and 2"); break; case 1: printf("Choice is 1"); break; case 2: printf("Choice is 2"); break; } return 0; }
Output:
Choice other than 1 and 2
4) The integral expressions used in labels must be constant expressions.
It means, the case labels are always constant.
// A program with variable expressions in labels #include <stdio.h> int main() { int x = 2; int arr[] = {1, 2, 3}; switch (x) { case arr[0]: printf("Choice 1\n"); case arr[1]: printf("Choice 2\n"); case arr[2]: printf("Choice 3\n"); } return 0; }
Output:
Compiler Error: case label does not reduce to an integer constant
5) The statements written above cases are never executed.
It means, after switch statement, the controller moves to the matching case labels, the statements written before case are not implemented.
// Statements before all cases are never executed #include <stdio.h> int main() { int x = 1; switch (x) { x = x + 1; // This statement is not executed case 1: printf("Choice is 1"); break; case 2: printf("Choice is 2"); break; default: printf("Choice other than 1 and 2"); break; } return 0; }
Output:
Choice is 1
6) Two case labels cannot have same value.
It means, two case labels are not have same case values.
// Program where two case labels have same value #include <stdio.h> int main() { int x = 1; switch (x) { case 2: printf("Choice is 1"); break; case 1+1: printf("Choice is 2"); break; } return 0; }
Output:
Compiler Error: duplicate case value
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above WhatsApp me.