Switch Statement in C

Last Updated :

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.

Comment
Next Article
Mithlesh Upadhyay Published 20 Feb, 2023 · 3 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Similar Reads

  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read