Join Regular Classroom : Visit ClassroomTech

Switch Case in C

It is a statement in C that allows to execute a specific code block. It is one of the many alternative ways.

Note: switch case follows a top-down approach.

Syntax :

switch (expression)
{
    case constant_1:
      // statements within the block
      break;

    case constant_2:
      // statement within the block
      break;
    .
    .
    .
    default:
      // default statements within the default block
}

Example : 1

//code to understand Switch case statement
//codewindow.in

#include <stdio.h>

int main(void) {
	int val;
	printf("\nEnter a value: ");
	scanf("%d",&val);
	
	switch(val)
	{
	    case 1: printf("\nSunday");
	            break;
	    case 2: printf("\nMonday");
	            break;
	    case 3: printf("\nTuesday");
	            break;
	    case 4: printf("\nWednesday");
	            break;
	   default: printf("\nOff-Day");
	   /* It is used for the other cases except 1,2,3,4 */
	            
	}
	return 0;
}
/*
For val= 1, Output will be Sunday 
For val= 2, Output will be Monday 
For val= 3, Output will be Tuesday 
For val= 4, Output will be Wednesday 
For any other value, Output will be Off-Day 

*/

Explanation:
For value 1, the expression is matched with
all the case values, the case value which matches with the expression i.e. case (1) and executes its statement and then breaks from the switch-case block.

For value 2, the expression is matched with all the case values, the case value which matches with the expression i.e. case (2) and executes its statement and then breaks from the switch-case block.


The same goes for other values too.

Example : 2

//code to understand Switch case Statement
//codewindow.in

#include <stdio.h>

int main(void) {
	int val;
	printf("\nEnter a value: ");
	scanf("%d",&val);
	
	switch(val)
	{
	    case 1: printf("\nSunday");
	 
	    case 2: printf("\nMonday");
	            
	    case 3: printf("\nTuesday");
	            
	    case 4: printf("\nWednesday");
	            
	   default: printf("\nOff-Day");
	   /* It is used for the other cases except 1,2,3,4 */
	            
	}
	return 0;
}

/*
For val= 1, Output will be --
Sunday
Monday
Tuesday
Wednesday
Off-Day

For val= 2, Output will be --
Monday
Tuesday
Wednesday
Off-Day

For val= 3, Output will be --
Tuesday
Wednesday
Off-Day

For val= 4, Output will be --
Wednesday
Off-Day

For any value, Output will be 
Off-Day 
*/

Explanation:
Here,
Since the break statement is not used in each case, it follows the next case after execution of the preferred block

When the value is 1, it executes the block with case value 1 and then follows the next case block (value 2) and continues, as there is
no break statement used to break the execution process including the default block.

When the value is 2, it executes the block with case value 2 (the second one) and then follows the next case block (value 3) and continues, as there is no break statement used to break the execution process including the default block.

When the value is 4, it executes the block with case value 4 (the second one) and then follows the next case block (default case block).

Example : 3

//code
//codewindow.in

#include <stdio.h>

int main(void) {
	int val;
	printf("\nEnter a value: ");
	scanf("%d",&val);
	
	switch(val)
	{
	    case 2: printf("\nSunday");
	 
	    case 1: printf("\nMonday");
	            break;
	    case 4: printf("\nTuesday");
	            
	    case 3: printf("\nWednesday");
	            break;
	   default: printf("\nOff-Day");
	   /* It is used for the other cases except 1,2,3,4 */
	            
	}
	return 0;
}

/*
For val= 1, Output will be --
Monday

For val= 2, Output will be --
Sunday
Monday

For val= 3, Output will be --
Wednesday

For val= 4, Output will be --
Tuesday
Wednesday

For any value, Output will be 
Off-Day 
*/

Explanation:
Here,
For value 1 it searches the case with the same value and executes it, hence executes case 1 (which is after 2, in this case) and then breaks out of the process as they break statement is mentioned.

For value 2 it searches the case with the same value and executes it, hence executes case 2 (which is before 1, in this case) since there is no break statement and follows a top-down approach it executes the next immediate case block i.e. 1 and after executing its statement it finally breaks off the process.

*Here it doesn’t follow any numbering system such as 1 then 2 then 3, it follows a
top-down approach.

For value 4 it searches the case with the same value and executes it, hence executes case with value 4 (which is before 3, in this case) since there is
no break statement and follows a top-down approach it executes the next immediate case block i.e. case (3) and after executing its statement it finally breaks off the process.

Example: 4

//code to understand switch case Statement
//codewindow.in
#include <stdio.h>

int main(void) {
	int val;
	printf("\nEnter a value: ");
	scanf("%d",&val);
	
	switch(val)
	{
	    
	    default: printf("\nOff-Day");
	    case 2: printf("\nSunday");
	 
	    case 1: printf("\nMonday");
	            break;
	    case 4: printf("\nTuesday");
	            
	    case 3: printf("\nWednesday");
	            break;
	   /* It is used for the other cases except 1,2,3,4 */
	            
	}
	return 0;
}
/*
For val= 1, Output will be --
Monday

For val= 2, Output will be --
Sunday
Monday

For val= 3, Output will be --
Wednesday

For val= 4, Output will be --
Tuesday
Wednesday

For any value, Output will be 
Off-Day 
Sunday
Monday
*/

Explanation:
Here,
For value 1 it executes the case block with value 1 and breaks the process.

For value 2 it executes the case block with value 2 i.e. case (2) since there is no break statement it immediately executes the next case block i.e. case (1) and then breaks off the process.

For value 4 it executes the case block with value 4 i.e. case (4) since there is no break statement it immediately executes the next case block i.e. case (3) and then breaks off the process.

For any other value it starts from the default value and execution of the process continues (case (2), case (1)) till it reaches a break statement which is after case (1), and the execution breaks.

Example : 5

//code to understand switch case statement
//codewindow.in

#include <stdio.h>

int main(void) {
	char ch;
	printf("\nEnter a character: ");
	scanf("%c",&ch);
	
	switch(ch)
	{
	    
	    case 'S': printf("\nSunday");
	              break;
	    case 'S': printf("\nMonday");
	              break;
	    case 'T': printf("\nTuesday");
	              break;
	    case 'W': printf("\nWednesday");
	              break;
	            
	    default: printf("\nOff-Day");
	   /* It is used for the other cases except 1,2,3,4 */
	            
	}
	return 0;
}
/*
Duplicate case value is not allowed in switch case
*/

Explanation:
Cases with the same value are not permitted in C. Hence it throws an error saying “Duplicate case value is not allowed in switch case

Example : 6

//code to understand switch case Statement
//codewindow.in

#include <stdio.h>

int main(void) {
	char ch;
	printf("\nEnter a character: ");
	scanf("%c",&ch);
	
	switch(ch)
	{
	    
	    case 'S': printf("\nSunday");
	              break;
	    case 49:  printf("\nMonday");
	              break;
	    case 'T': printf("\nTuesday");
	              break;
	    case 'W': printf("\nWednesday");
	              break;
	            
	    default: printf("\nOff-Day");
	   /* It is used for the other cases except 1,2,3,4 */
	            
	}
	return 0;
}

/*
Input: 12
Output: Off-Day
ch='1'
Ascii of '1' = 49
So, we can write as case '1' or case 49
*/

Explanation:
Here the value input is 12, thus none of the case value matches with it, hence it executes the default block and then breaks out of the process.


Also checkout