Home >>C Tutorial >C Switch Statement

C Switch Statement

C Switch statements

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;    
}    

There are certain rules for the Switch statements in the C language which are as follows:

  • The switch expression in C must be of a character or integer type.
  • The case value in switch statements must be a character or integer constant.
  • The case value in switch statements can only be used inside the switch statement.
  • The break statement in switch statement case is not a must. All the cases will be executed that are present after the matched case, if there is no break statement found in the case. This is called the fall through state of C switch statement.

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;

Here is the flowchart of the switch statements:

Let's understand the functioning of the switch case statements in C language

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.

Here is a simple example of the switch statements in the C language:

#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;  
}    
Output :
enter a number:4
number is not equal to 10, 50 or 500
enter a number:50
number is equal to 50

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 ");  
    }   
          
}  
Output : hello

C Switch statement is a fall-through(without using break)

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;  
}    
Output :
enter any number:2
number is equal to 2
number is equal to 4
number is equal to 6
number is not equal to 2, 4 or 6

Nested switch case statement in C

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;  
}  
Output :
The value of a is : 2
The value of b is : 4

No Sidebar ads