Home >>C Tutorial >C Switch Statement
The switch statement in C language allows the users to perform multiple operations for different potential values of a single variable known as switch variable. With Switch statements, users can define various statements in the multiple cases for the different values of a single variable.
Here is the syntax of the switch statements in the C language:
switch(expression) { case value1: //code to be executed; break; //optional case value2: //code to be executed; break; //optional ...... default: code that is be executed if all the cases are not matched; }
Here are certain examples that will help you understand the switch statements:
int x,y,z; char a,b; float f;
Valid Switch | Invalid Switch | Valid Case | Invalid Case |
---|---|---|---|
switch(x) | switch(f) | case 3; | case 2.5; |
switch(x>y) | switch(x+2.5) | case 'a'; | case x; |
switch(a+b-2) | case 1+2; | case x+2; | |
switch(func(x,y)) | case 'x'>'y'; | case 1,2,3; |
The first step involves evaluation of the integer expression specified in the switch statement. This obtained value is then matched sequence wise with the constant values provided in the different cases. All the statements specified in that case are executed along with all the cases present after that case including the default statement, if and only if a match is found. Similar values of two cases is not possible. All the cases present after that will be skipped, and the control comes out of the switch, if and only if the matched case includes a break statement. Or else, all the cases that are following the matched case will be executed.
#include<stdio.h> int main() { int number=0; printf("enter a number:"); scanf("%d",&number); switch(number){ case 10: printf("number equals to 10"); break; case 50: printf("number equal to 50"); break; case 100: printf("number equal to 100"); break; default: printf("number not equal to 10, 50 or 500"); } return 0; }
Here is another example of the switch statement in C Language:
#include <stdio.h> int main() { int x = 10, y = 5; switch(x>y && x+y>0) { case 1: printf("hello"); break; case 0: printf("bye"); break; default: printf(" Hello bye "); } }
In the C language, switch statement is generally fall through; it simply means that if the user didn't use a break statement in the switch case then all the cases after the matching case will be executed.
Here is an example for the same:
#include<stdio.h> int main() { int num=0; printf("enter any number:"); scanf("%d",&num); switch(num) { case 2: printf("number is equal to 2\n"); case 4: printf("number is equal to 4\n"); case 6: printf("number is equal to 6\n"); default: printf("number is not equal to 2, 4 or 6"); } return 0; }
Users can use as many switch statement as they want inside a switch statement. These types of statements are known as nested switch case statements.
Here is an example for the same:
#include <stdio.h> int main () { int a = 2; int b = 4; switch(a) { case 1: printf("the value of a evaluated in outer switch: %d\n",a); case 2: switch(b) { case 2: printf("The value of b evaluated in nested switch: %d\n",b); } } printf("The value of a is : %d\n", a ); printf("The value of b is : %d\n", b ); return 0; }