Switch Statement in C Programming

This statement is similar to ‘if’ statement. This is used when we have to select a set of statements to be executed depending on the expression results. We can use if or nested if statements, but it increases the complexity of program as well as reduces the readability. In the switch..case statement, we check for the condition, which results can have any of the multiple values. For each of the result value we might have different set of statements to be executed.

Let us first see the general syntax for switch..case statement. It first has expression which can be any expression with arithmetic operation, logical operation or any other operators. Result of this expression is later compared in the ‘case’ expressions. If it matches the result, then those expressions/ statements are executed.

switch (expression) {
	case constant1:
		expressions / statements;
	case constant2:
		expressions / statements;
		….
	case constantN:
		expressions / statements;
	default:
		expressions / statements;
}

Consider the example of displaying grades for students depending grades entered by the user. We can do the same using if statement, but we have to write if, else if, else multiple times to address all the grades. This will increase the complexity of the program. If we use switch statement then it will be easy to understand and evaluate.

#include <stdio.h> 

void main () {
	char chrGrade;

	printf ("\nPlease enter the grade:");
	chrGrade = getchar ();

	switch (chrGrade) {
	case 'A':
		printf ("\nGrade is A");
	case 'B':
		printf ("\nGrade is B");
	case 'C':
		printf ("\nGrade is C");
	default:
		printf ("\nThis is the default grade - D");
	}
}

Here expression can be any expression with operator or a constant variable which should result in constant values. It can be integer, float, character or string. Case statement will compare the result of these expressions and if it matches, their results will be displayed in the screen. The datatype for the values in case statement should be same as that of switch statement. it evaluates the expression in the switch expression and jumps to the respective case to evaluate further. If it does not find the values in the case statements, then it comes out of the switch statement. But if we need to have any message or expression to be evaluated when no option is found, then we have to set default case. This default case us set at the end of all case statements. This is because, switch statement is evaluated sequentially – one case after the other. Hence when it evaluates cases and finds default at beginning or in between, it thinks that the value it is searching for does not exist and prints/ evaluates default statements, even though it has case statements at the end. This is not expected from any program. Hence we put default statement at the end.

In the result screen we can note that it has resulted in correct output when grade is entered as ‘D’. But what is the result when we enter any other values other than default – like ‘A’, ‘B’ or ‘C’? It prints the respective output as well as its subsequent outputs till default! Why did it print so? The switch statement does not consider the case statement as different conditions to be evaluated. It considers all the cases as sequence of statements to be evaluated. It does not jump out of the case when it executes its respective statements. We have to explicitly break it executing rest of the case statements. This is done by using break statement. Hence above program will change as below to have break statement after each case statement.

#include <stdio.h> 

void main () {
	char chrGrade;

	printf ("\nPlease enter the grade:");
	chrGrade = getchar ();

	switch (chrGrade) {
	case 'A':
		printf ("\nGrade is A");
		break;
	case 'B':
		printf ("\nGrade is B");
		break;
	case 'C':
		printf ("\nGrade is C");
		break;
	default:
		printf ("\nThis is the default grade - D");
		break;
	}
}

Now the program shows the correct output for each grade that has been entered. Please note here that case statements act as labels, hence we do not have any curly brackets in case statements. Thus we need not have to have any brackets when we write more than one line of code within each case statement. Only the break statement within it makes it as end of case statement and makes it to jump out of the switch statement. The only curly brackets for switch indicate beginning and end of switch statements.

In above example, we have considered that user will enter the grade in upper case alone. Suppose they can enter grade in both upper and lower cases. Then we might have to write the case statement for both upper and lower case with same message.

switch (chrGrade) {
	case 'A':
		printf ("\nGrade is A");
		break;
	case ‘a’:
		printf ("\nGrade is A");
		break;
	case 'B':
		printf ("\nGrade is B");
		break;
	case 'b':
		printf ("\nGrade is B");
		break;
	case 'C':
		printf ("\nGrade is C");
		break;
	case 'c':
		printf ("\nGrade is C");
		break;
	default:
		printf ("\nThis is the default grade - D");
		break;
}

Like we have nested if statements, we can also have nested switch statements. Consider the program to enter the day type and day number and get respective message displayed on the screen. These program first checks for the day type –whether it is a working day or holiday and then checks which day of the week and displays the message. It uses two switch statements  -one to check day type and other one to check day. These two switch statements are nested since we have to display the messages depending in both day and day type. Break statement has to be used after each case statement of the outer switch statement. Else it will display the default statement of outer switch statement along with the proper message.

#include <stdio.h> 

void main() {
	int intDay;
	char chrDayType;

	printf("\nPlease enter the day type (H-Holiday, W-Working):");
	chrDayType = getchar ();
	printf ("\nPlease enter the Day (1-7):");
	scanf("%d", &intDay);

	switch (chrDayType) {
	case 'H':
		switch (intDay){
		case 1:
			printf("It is Sunday and is a Holiday!"); break;
		case 2:
			printf("It is Monday and is a Holiday!"); break;
		case 3:
			printf("It is Tuesday and is a Holiday!"); break;
		case 4:
			printf("It is Wednesday and is a Holiday!"); break;
		case 5:
			printf("It is Thursday and is a Holiday!"); break;
		case 6:
			printf("It is Friday and is a Holiday!"); break;
		case 7:
			printf("It is Saturday and is a Holiday!"); break;
		default: printf("It is not a valid Day!"); 
		}
		break;
	case 'W':
		switch (intDay){
		case 1:
			printf("It is Sunday and is a Working Day!"); break;
		case 2:
			printf("It is Monday and is a Working Day!"); break;
		case 3:
			printf("It is Tuesday and is a Working Day!"); break;
		case 4:
			printf("It is Wednesday and is a Working Day!"); break;
		case 5:
			printf("It is Thursday and is a Working Day!"); break;
		case 6:
			printf("It is Friday and is a Working Day!"); break;
		case 7:
			printf("It is Saturday and is a Working Day!"); break;
		default: printf("It is not a valid Day!");
		}
		break;
	default: printf("It is not a valid Day Type!");
	}
}
Translate »