Skip to content
Home » C Programming » Control Flow » The C Switch Statement

The C Switch Statement

Introduction

The switch statement is a control flow statement that allows a C program to evaluate an expression and based on its value, execute a specific block of code among several alternatives. It has some similarities with the if statement but the switch statement does have certain advantages over the if statement in certain use cases.

The Switch Statement Syntax

With brackets around case:

switch (expression)
{
	case constant1:
	{
		/* code here */
		break;
	}
	case constant2:
	{
		/* code here */
		break;
	}
	/* more cases here */
	default:    /* optional */
	{
		/* code here */
	}
}

Without brackets around case:

switch (expression)
{
	case constant1:
		/* code here */
		break;
	case constant2:
		/* code here */
		break;
	/* more cases here */
	default:    /* optional */
		/* code here */
}

Things to note here:

the expression must be one of these data types:

  • int
  • short
  • long
  • long long
  • enum
  • char

This is unlike a normal if statement, where you can use any data type to construct an expression and chain them with logical AND or OR operators. The switch statement won’t work with any other data types and you cannot use logical AND or OR operators inside a switch statement. The following is a simple example that prints day of week according to the integer called day.

#include <stdio.h>

int main()
{
    int day = 3;
    switch (day) 
    {
        case 1:
            printf("Monday\n");
            break;
        case 2:
            printf("Tuesday\n");
            break;
        case 3:
            printf("Wednesday\n");
            break;
        default:
            printf("Other day\n");
    }
    return 0;
}

The Break Keyword in C Switch Statement

Similar to a C loop, switch statement also uses the break keyword to break out of the entire switch statement. You must use break keyword within the case block if you are finished with your code logic. Otherwise, a case fall through will occur, which causes all the case blocks below the current case to be executed as well unless a break keyword is encountered. This may be intentional or accidental depending on your application logic.

The following example illustrates a C switch statement with intentional case fall through with enum:

void switchExample(void)
{
	char selector = 'g';
	switch (selector)
	{
		case 'a':
			printf("apple\n");
			break;
		case 'b':
			printf("banana\n");
			break;
		case 'c':
			printf("car\n");
			break;
		case 'd':
			printf("dog\n");
			break;
		case 'f':
		case 'g':
		case 'h':
		case 'i':
			printf("fish, gorilla, hat, igloo\n");
			break;
		default:
			printf("unknown letter\n");
	}
}

In the example above, case f, g, h, i will fall through and processed by line 22 , which will print fish, gorilla, hat, igloo altogether for all 4 cases. Let’s look at another case using enum as data type and with accidental case fall through:

typedef enum dayOfWeek
{
	SUNDAY = 0,
	MONDAY,
	TUESDAY,
	WEDNESDAY,
	THURSDAY,
	FRIDAY,
	SATURDAY
} Day;
void switchExample2(void)
{
	Day today = WEDNESDAY;
	switch(today)
	{
		case SUNDAY:
			printf("today is Sunday(%d)\n", today);
			break;
		case MONDAY:
			printf("today is Monday(%d)\n", today);
			break;
		case TUESDAY:
			printf("today is Tuesday(%d)\n", today);
			break;
		case WEDNESDAY:
			printf("today is Wednesday(%d)\n", today);
		case THURSDAY:
			printf("today is Thursday(%d)\n", today);
		case FRIDAY:
			printf("today is Friday(%d)\n", today);
		case SATURDAY:
			printf("today is Saturday(%d)\n", today);
			break;
		default:
			printf("invalid day(%d)\n", today);
			break;
	}
}

The example above should print the day of week according to the value of an enum (“today”), but it prints undesirable outputs due to the missing break keyword. Note that the case blocks below WEDNESDAY are executed as well even if the condition does not match their case. This does not matter, the case fall through takes the precedence here.

today is Wednesday(3)
today is Thursday(3)
today is Friday(3)
today is Saturday(3)

When to Use What?

  • Use switch when:
    • You have a single expression with multiple constant values to check.
    • You want to improve code readability by expressing a series of conditions concisely.
  • Use if when:
    • You need to check a boolean condition that involves various comparisons and logical operations.
    • You have multiple independent conditions that don’t fit well into a switch structure.

And… Be careful with case fall through…

Related Pages

Join the conversation

Your email address will not be published. Required fields are marked *