Switch Statement in C (original) (raw)

Last Updated : 7 Nov, 2025

**C switch statement is a conditional statement that allows you to execute different code blocks based on the value of a variable or an expression. It is often used in place of if-else ladder when there are multiple conditions.

C `

#include <stdio.h>

int main() {

// Switch variable
int var = 1;

// Switch statement
switch (var) {
case 1:
    printf("Case 1 is Matched.");
    break;
case 2:
    printf("Case 2 is Matched.");
    break;
case 3:
    printf("Case 3 is Matched.");
    break;
default:
    printf("Default case is Matched.");
    break;
}
return 0;

}

`

**Explanation: In this program, the switch statement evaluates the variable **var. Since **var is **1, the code in **case with value 1 executes, printing "**Case 1 is Matched." and then exiting the switch block due to the break statement. Other cases and the default case are skipped.

Syntax of switch

C `

switch (expression) { case value1:

    // Code_block1
    break;

case value2:

    // Code_block2
    break;

default:
    // Default_code_block

}

`

Here,

**Flowchart of C switch Statement

switch case in c

Working of Switch Statement

The working of the switch statement in C is as follows:

Examples of Switch Statement

The below programs show some use cases of switch statement in practical scenario:

C `

#include <stdio.h>

int main() {

  // Day number
int day = 2;

  // Switch statement to print the name of the day
  // on the basis of day number
switch (day) {
case 1:
    printf("Monday");
    break;
case 2:
    printf("Tuesday");
    break;
case 3:
    printf("Wednesday");
    break;
case 4:
    printf("Thursday");
    break;
case 5:
    printf("Friday");
    break;
case 6:
    printf("Saturday");
    break;
case 7:
    printf("Sunday");
    break;
default:
    printf("Invalid Input");
    break;
}

return 0;

}

`

Simple Calculator using Switch Statement

C `

#include <stdio.h> #include <stdlib.h>

int main() { // two number int x, y; // Variable that select operation to perform // i.e. switch variable char choice;

  // Take input
printf("Enter the Operator (+,-,*,/)\n");
scanf(" %c", &choice);

printf("Enter the two numbers: ");
scanf("%d %d", &x, &y);

// switch case with operation for each operator
switch (choice) {
case '+':
    printf("%d + %d = %d\n", x, y, x + y);
    break;

case '-':
    printf("%d - %d = %d\n", x, y, x - y);
    break;

case '*':
    printf("%d * %d = %d\n", x, y, x * y);
    break;
case '/':
    printf("%d / %d = %d\n", x, y, x / y);
    break;
default:
    printf("Invalid Operator Input\n");
}

return 0;

}

`

**Output

Enter the Operator (+,-,*,/)

**C switch Statement without Break

The**break keyword is used to stop the execution inside a switch block. It helps to terminate the switch block and break out of it. If omitted, execution will continue on into the next case.

C `

#include <stdio.h>

int main() { int var = 2;

// switch case without break
switch (var) {
case 1:
    printf("Case 1 is executed.\n");
case 2:
    printf("Case 2 is executed.\n");
case 3:
    printf("Case 3 is executed.\n");
case 4:
    printf("Case 4 is executed.");
}
return 0;

}

`

Output

Case 2 is executed. Case 3 is executed. Case 4 is executed.

**Nested switch Statement

Nesting of switch statements is allowed, which means you can have switch statements inside another switch. However nested switch statements should be avoided as it makes the program more complex and less readable.

C++ `

#include <stdio.h>

int main() { int outerChoice = 1; int innerChoice = 2;

// Outer switch to choose the category
switch(outerChoice) {
    case 1:
        printf("Category 1 selected\n");

        // Inner switch to choose the option under category 1
        switch(innerChoice) {
            case 1:
                printf("Option 1 selected under Category 1\n");
                break;
            case 2:
                printf("Option 2 selected under Category 1\n");
                break;
            default:
                printf("Invalid option under Category 1\n");
        }
        break;

    case 2:
        printf("Category 2 selected\n");

        // Inner switch to choose the option under category 2
        switch(innerChoice) {
            case 1:
                printf("Option 1 selected under Category 2\n");
                break;
            case 2:
                printf("Option 2 selected under Category 2\n");
                break;
            default:
                printf("Invalid option under Category 2\n");
        }
        break;

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

return 0;

}

`

Output

Category 1 selected Option 2 selected under Category 1