A switch statement allows a variable or value of an expression to be tested for equality against a list of possible case values and when match is found, the block of code associated with that case is executed.
Syntax of Switch Statement
switch(expression) {
case constant1 :
/* Code to be executed when value of expression equals constant1 */
statement;
break;
case constant2 :
/* Code to be executed when value of expression equals constant2 */
statement;
break;
case constant3 :
/* Code to be executed when value of expression equals constant3 */
statement;
break;
default : /* Optional */
statement;
}
case constant1 :
/* Code to be executed when value of expression equals constant1 */
statement;
break;
case constant2 :
/* Code to be executed when value of expression equals constant2 */
statement;
break;
case constant3 :
/* Code to be executed when value of expression equals constant3 */
statement;
break;
default : /* Optional */
statement;
}

Important Points about Switch Statement
- Switch case performs equality check of the value of expression/variable against the list of case values.
- The expression in switch case must evaluates to return an integer, character or enumerated type.
- You can use any number of case statements within a switch. The expression value is compared with the constant after case.
- The data type of the value of expression/variable must be same as the data type of case constants.
- The break statement is optional.The break statement at the end of each case cause switch statement to exit. If break statement is not used, all statements below that case statement are also executed until it found a break statement.
- The default code block gets executed when none of the case matches with expression. default case is optional and doesn't require a break statement.
C Program to print grade of a student using Switch case statement
#include <stdio.h> int main(){ int marks; printf("Enter your marks between 0 to 100\n"); scanf("%d", &marks); switch(marks/10){ case 10 : case 9 : /* Marks between 90-100 */ printf("Your Grade : A\n" ); break; case 8 : case 7 : /* Marks between 70-89 */ printf("Your Grade : B\n" ); break; case 6 : /* Marks between 60-69 */ printf("Your Grade : C\n" ); break; case 5 : case 4 : /* Marks between 40-59 */ printf("Your Grade : D\n" ); break; default : /* Marks less than 40 */ printf("You failed\n" ); } return 0; }Above program check whether a student passed or failed in examination using if statement.
Output
Enter your marks between 0 to 100 55 Your Grade : D
Enter your marks between 0 to 100 99 Your Grade : A
Enter your marks between 0 to 100 30 You failedAdvantage of Switch Statement
- The complexity of the program increases as the number of constants to compare increases. Switch statement provides a way to write easy to manage and readable code.
- Switch statement provides a default case handler when no case matches.
- We can nest switch statements like if else statements.
- Switch statements can only be used to perform equality comparison, we cannot perform conditional checks using relational operators like >, <, >== etc.