Switch Statement

The switch statement is used to execute one of several blocks of code based on the value of a variable.
Expression: The expression following the switch can be an integer or character expression. The expression must be evaluated with an integer or a character.
Case labels: Case labels must be unique and compile-time constants. Variable or changeable values are not allowed as case labels.
Default case: The default case is optional and can be used when no case matches.
Break statement: The break statement is optional but often necessary. It indicates the end of a particular case.

Switch Statement Syntax


switch (expression) {
    case constant1:
        // Code to execute if expression == constant1
        break;
    case constant2:
        // Code to execute if expression == constant2
        break;
    ...
    default:
        // Code to execute if no case matches
}

Example:1


#include <stdio.h>
int main() {
    int choice = 2;
    switch (choice) {
        case 1:
            printf("Choice is 1\n");
            break;
        case 2:
            printf("Choice is 2\n");
            break;
        default:
            printf("Invalid choice\n");
    }
    return 0;
}

Output

Choice is 2

Example 2: Menu Selection


#include <stdio.h>
int main() {
    int choice;
    printf("Menu:\n");
    printf("1. Pizza\n");
    printf("2. Burger\n");
    printf("3. Pasta\n");
    printf("4. Exit\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            printf("You chose Pizza.\n");
            break;
        case 2:
            printf("You chose Burger.\n");
            break;
        case 3:
            printf("You chose Pasta.\n");
            break;
        case 4:
            printf("Exiting the program.\n");
            break;
        default:
            printf("Invalid choice! Please select from the menu.\n");
    }

    return 0;
}

Output

Menu: 1. Pizza 2. Burger 3. Pasta 4. Exit Enter your choice: 2 You chose Burger.

Example 3: Vowel or Consonant


#include <stdio.h>
int main() {
    char ch;
    printf("Enter a character: ");
    scanf(" %c", &ch);

    switch (ch) {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
        case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U':
            printf("%c is a vowel.\n", ch);
            break;
        default:
            printf("%c is a consonant.\n", ch);
    }

    return 0;
}

Output

Enter a character: e e is a vowel.

Fall-through Behavior:

If the break statement is omitted, execution continues to the next case, even if it doesn't match.


#include <stdio.h>
int main() {
    switch (2) {
        case 1:
            printf("One\n");
        case 2:
            printf("Two\n");
        case 3:
            printf("Three\n");
    }

    return 0;
}

Output

Two Three

Nested switch Statement

A nested switch statement in C refers to a switch statement inside another switch statement. This is useful for decision-making where one switch depends on another or where there are multiple levels of conditions to check.

Syntax


switch (expression1) {
    case constant1:
        // Outer switch case
        switch (expression2) {
            case constantA:
                // Inner switch case A
                break;
            case constantB:
                // Inner switch case B
                break;
            default:
                // Inner switch default
        }
        break;
    case constant2:
        // Outer switch case
        break;
    default:
        // Outer switch default
}

Example: Create a menu where the user selects a category, and based on the category, they can choose a specific item.


#include <stdio.h>
int main(){
int category, item;
printf("Select a category:\n");
printf("1. Beverages\n");
printf("2. Snacks\n");
printf("Enter your choice: ");
scanf("%d", &category);

switch (category) {
    case 1:  // Beverages
        printf("Select a beverage:\n");
        printf("1. Tea\n");
        printf("2. Coffee\n");
        printf("Enter your choice: ");
        scanf("%d", &item);

        switch (item) {
            case 1:
                printf("You selected Tea.\n");
                break;
            case 2:
                printf("You selected Coffee.\n");
                break;
            default:
                printf("Invalid beverage choice.\n");
        }
        break;

    case 2:  // Snacks
        printf("Select a snack:\n");
        printf("1. Sandwich\n");
        printf("2. Burger\n");
        printf("Enter your choice: ");
        scanf("%d", &item);

        switch (item) {
            case 1:
                printf("You selected Sandwich.\n");
                break;
            case 2:
                printf("You selected Burger.\n");
                break;
            default:
                printf("Invalid snack choice.\n");
        }
        break;

    default:
        printf("Invalid category choice.\n");
}

return 0;
}

Output 1 (User chooses a beverage):

Select a category: 1. Beverages 2. Snacks Enter your choice: 1 Select a beverage: 1. Tea 2. Coffee Enter your choice: 2 You selected Coffee.

Output 2 (User chooses an invalid item):

Select a category: 1. Beverages 2. Snacks Enter your choice: 2 Select a snack: 1. Sandwich 2. Burger Enter your choice: 3 Invalid snack choice.