Home C C++ Java Python Perl PHP SQL JavaScript Linux Selenium QT Online Test

Home » C Programming » Tutorial » Switch Cases in C

Switch Statement in C Programming

A switch case statements is one of the conditional statement in C programming. It is similar to "if else" with few differences. It allows a variable to be tested for equality against a list of value. Each value is known as case. It contains case, default & break.

Example on Switch Statement

#include<stdio.h>

int main()
{
char ch;

printf("Press any character from a - z : ");
scanf("%c", &ch);

switch (ch)
{
	case 'a':
	 		printf("You pressed 'a'\n");
	 		break;
	case 'b':
            printf("You pressed 'b'\n");
            break;
 	case 'c':
            printf("You pressed 'c'\n");
            break;               
	default:
			printf("Invalid choice\n");
			break;
}
return 0;
}

How does Switch statement works?

If you look at the above example; "ch" is the input char for switch statement. Different cases inside switch like "case 'a'" work likes "if" and if the value of "ch" is "a" then "You pressed 'a'" is executed. Cases are followed by break statement to terminate it, otherwise control would go in other cases until it finds break.

Important things to remember about Switch

  • Switch can have n number of 'cases'
  • Switch can have only one 'default'
  • It is good practice to terminate each case by 'break'
  • One case can have block of statements
  • Switch can have nested switch also

Clubbing in Switch Statement

When we have multiple cases followed by one another and break after last case then this way of putting case together is known as Clubbing in Switch

Example on Clubbing

#include<stdio.h>

int main()
{
char ch;

printf("Press any character from a - i : ");
scanf("%c", &ch);

switch (ch)
{
	case 'a':
	case 'b':
 	case 'c':
            printf("You pressed something from 'a - c'\n");
            break;     
    case 'd':
	case 'e':
 	case 'f':
            printf("You pressed something from 'd - f'\n");
            break;          
    case 'g':
	case 'h':
 	case 'i':
            printf("You pressed 'g - i'\n");
            break;          
            
	default:
			printf("Invalid choice\n");
			break;
}
return 0;
}

Nested Switch

C programming allows nested switch; It means we can have n level of nested switch statement.

Example on Nested Switch

#include<stdio.h>

int main()
{
char ch;
char sex;
printf("\n Press A for Illinois State");
printf(" \n Press B for California State");
printf("\n Enter your choice : ");
scanf(" %c",&ch);

switch (ch)
{
case 'A':		 
	 printf("\n Illinois State Resident");
	 printf("\n Press M for Male");
	 printf("\n Press F for female");
	 printf("\n Enter your choice : ");
	 scanf(" %c",&sex);
	 
	 switch(sex)
	 {
	 case 'M': printf("You are Male. You will get 10 percent discount");
			   break;
	 case 'F': printf("You are FeMale. You will get 15 percent discount");
			   break;   
	 default: printf("\n Invalid Choice");
	 }
	 break;
		 
case 'B':
	 printf("\n California State Resident");
	 printf("\n Press M for Male");
	 printf("\n Press F for female");
	 printf("\n Enter your choice : ");
	 scanf(" %c",&sex);
	 
	 switch(sex)
	 {
	 case 'M': printf("You are Male. You will get 7 percent discount");
			   break;
	 case 'F': printf("You are FeMale. You will get 9 percent discount");
			   break;   
	 default: printf("\n Invalid Choice");
	 }
	 break;
default:
	printf("Invalid choice\n");
	break;
}
return 0;
}