Switch Statement in C

By | February 20, 2023

Prerequisite – Control Statements in C
The “switch” statement in C is a conditional statement that allows you to test the value of a variable against a list of possible values. It can be a useful alternative to a series of “if-else” statements when you have multiple conditions to test.

Syntax:
The basic syntax of a “switch” statement in C is:

switch (expression) {
    case value1:
        // code to be executed if expression equals value1
        break;
    case value2:
        // code to be executed if expression equals value2
        break;
    // more case statements can be added here
    default:
        // code to be executed if expression doesn't match any of the values
}

Here, expression is a variable or expression that you want to test, and each case statement represents a possible value that the expression might have. If the expression matches one of the values, the corresponding code block is executed. If none of the cases match, the code in the default block is executed (which is optional).

Examples:
Here are some examples of using the “switch” statement in C:

Example-1: Determine the number of days in a given month

#include <stdio.h>

int main() {
    int month, days;

    printf("Enter a month (1-12): ");
    scanf("%d", &month);

    switch (month) {
        case 2:
            days = 28;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            days = 30;
            break;
        default:
            days = 31;
            break;
    }

    printf("Number of days in month %d: %d", month, days);

    return 0;
}

Output:

Enter a month (1-12): 4
Number of days in month 4: 30

In this example, the “switch” statement is used to determine the number of days in a given month. The user is prompted to enter a month (represented by an integer between 1 and 12), and the value is stored in the month variable. The switch statement then checks the value of month against several possible cases. If month is equal to 2, the number of days is set to 28. If month is equal to 4, 6, 9, or 11, the number of days is set to 30. If month doesn’t match any of the cases, the default value of 31 is used. Finally, the number of days is printed to the console.

Example-2: Convert a letter grade to a number grade

#include <stdio.h>

int main() {
    char grade;
    int number_grade;

    printf("Enter a letter grade: ");
    scanf("%c", &grade);

    switch (grade) {
        case 'A':
            number_grade = 90;
            break;
        case 'B':
            number_grade = 80;
            break;
        case 'C':
            number_grade = 70;
            break;
        case 'D':
            number_grade = 60;
            break;
        case 'F':
            number_grade = 50;
            break;
        default:
            printf("Invalid grade entered.\n");
            return 1;
    }

    printf("The equivalent number grade is %d.\n", number_grade);

    return 0;
}

Output:

Enter a letter grade: B
The equivalent number grade is 80.

In this example, the user is prompted to enter a letter grade, which is stored in the variable grade. A “switch” statement is used to check the value of grade and assign a corresponding number grade to the variable number_grade. If an invalid grade is entered, the program outputs an error message and returns a non-zero exit code. Finally, the program outputs the equivalent number grade.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.