C switch case

Summary: in this tutorial, you will learn how to use the C switch...case statement to execute a code block based on a selection of multiple choices.

Introduction to C switch…case statement

The switch...case statement allows you to control complex conditional and branching operations. The switch...case statement transfers control to a statement within its body based on a condition.

The following shows the syntax of the switch...case statement:

switch (expression) { case value1: // statement1 break; case value2: // statement2 break; default: // default-statement break; }
Code language: C++ (cpp)

How the switch...case statement works.

The case statement

The switch...case statement allows you to check the expression against a set of values specified in the case statements. C requires that the expression needs to evaluate to an integer.

The switch...case statement evaluates the expression. If the result matches a value specified in a case statement, the switch...case statement executes the corresponding case statement.

A switch...case statement can have any number of case statements. However, the values in the case statements need to be integer constants and unique.

The break statement

The break statement ends the processing of a particular case statement within the switch statement.

The break statement is optional. If you don’t specify the break statement, the switch statement continues to the next case statement until it encounters a break statement or until it reaches the end of the switch...case statement.

The default statement

The switch...case statement will execute the default statement if the result of the expression doesn’t match any value specified in the case statements.

If you skip the default statement and no value in the case statement matches, the switch statement will not execute any statement within its body.

A switch statement can have at most one default statement. By convention, the default statement appears at the end of the switch statement. However, the default statement may appear anywhere within the switch statement.

The following flowchart illustrates how the switch...case statement works:

c switch case

C switch…case statement example

The following example uses the switch…case statement to display a weekday based on a weekday number from 1 to 7:

#include <stdio.h> int main() { // prompt for a weekday number int day; printf("Enter a day (1-7): "); scanf("%d", &day); // display the weekday switch (day) { case 1: printf("Sunday\n"); break; case 2: printf("Monday\n"); break; case 3: printf("Tuesday\n"); break; case 4: printf("Wednesday\n"); break; case 5: printf("Thursday\n"); break; case 6: printf("Friday\n"); break; case 7: printf("Saturday\n"); break; default: printf("Please enter a valid weekday number.\n"); } return 0; }
Code language: C++ (cpp)

How it works.

First, prompt for a weekday number from 1 to 7:

// prompt for a weekday number int day; printf("Enter a day (1-7): "); scanf("%d", &day);
Code language: C++ (cpp)

Second, display the corresponding weekday based on a weekday number based on the input weekday number. If the input number is 1, 2, 3, … 7, the case statement shows Sunday, Monday, Tuesday, … Friday. If the input number is not from 1 to 7, the default statement shows an error message.

switch (day) { case 1: printf("Sunday\n"); break; case 2: printf("Monday\n"); break; case 3: printf("Tuesday\n"); break; case 4: printf("Wednesday\n"); break; case 5: printf("Thursday\n"); break; case 6: printf("Friday\n"); break; case 7: printf("Saturday\n"); break; default: printf("Please enter a valid weekday number.\n"); }
Code language: C++ (cpp)

Matching multiple values

Sometimes, you want to match the expression of the switch…case statement with multiple values instead of one. In this case, you can stack up the case statements.

For example, in the above program, if the weekday number is 1 and 7, you want to display the message It's a weekend. And if the weekday is from 2 to 6, you want to display the message "It's a working day".

The following program illustrates the logic:

#include <stdio.h> int main() { // prompt for a weekday number int day; printf("Enter a day (1-7): "); scanf("%d", &day); // display the weekend or working day switch (day) { case 1: case 7: printf("It's weekend!\n"); break; case 2: case 3: case 4: case 5: case 6: printf("It's a working day.\n"); break; default: printf("Please enter a valid weekday number.\n"); } return 0; }
Code language: C++ (cpp)

Summary

  • Use the C switch...case statement to control complex conditional and branching operations.
Was this tutorial helpful ?