Switch Statement in C: Control Flow with Multiple Cases

The switch statement in C enables you to test a variable against multiple values, known as cases. Each case is evaluated for equality with the variable, allowing specific code to execute based on which case matches. This structured approach simplifies complex conditional statements, enhancing code readability and maintainability.



Switch Statement in C

A switch statement allows testing a variable against multiple values, called cases. Each case is checked for equality with the variable, and specific code is executed based on the match.

Syntax

Syntax

switch (expression) {
    case value1:
        // code
        break;
    case value2:
        // code
        break;
    default:
        // code
}

How It Works

The switch statement evaluates an expression and compares it with case values. If a match is found, the corresponding block is executed. A break statement exits the switch block. A default case handles unmatched values.

Flowchart of switch-case Statement

Rules

  • The expression must be integral or character type.
  • Each case must have a constant expression of the same type as the switch expression.
  • Statements in each case execute until a break is encountered.
  • If no break, execution continues to the next case.
  • default case is optional and should be last.

Examples

Example 1: Greeting Message

#include 
int main() {
    char ch = 'e';
    printf("Time code: %c\n", ch);
    switch (ch) {
        case 'a': printf("Good Afternoon\n"); break;
        case 'e': printf("Good Evening\n"); break;
        case 'm': printf("Good Morning\n"); break;
    }
    return 0;
}
Output

Time code: e
Good Evening
Example 2: Without Break

#include 
int main() {
    char ch = 'a';
    printf("Time code: %c\n", ch);
    switch (ch) {
        case 'a': printf("Good Afternoon\n");
        case 'e': printf("Good Evening\n");
        case 'm': printf("Good Morning\n");
    }
    return 0;
}
Output

Time code: a
Good Afternoon
Good Evening
Good Morning
Example 3: Grade Checker

#include 
int main() {
    char grade = 'B';
    switch (grade) {
        case 'A': printf("Outstanding!\n"); break;
        case 'B': printf("Excellent!\n"); break;
        case 'C': printf("Well Done\n"); break;
        case 'D': printf("You passed\n"); break;
        case 'F': printf("Better try again\n"); break;
        default: printf("Invalid grade\n");
    }
    printf("Your grade is %c\n", grade);
    return 0;
}
Output

Excellent!
Your grade is B
Example 4: Menu-based Calculator

#include 
int main() {
    int a = 10, b = 5, op = 1;
    float result;
    switch (op) {
        case 1: result = a + b; break;
        case 2: result = a - b; break;
        case 3: result = a * b; break;
        case 4: result = a / b; break;
        default: printf("Invalid operation\n"); return 0;
    }
    printf("Result: %.2f\n", result);
    return 0;
}
Output

Result: 15.00

Combining Multiple Cases

Multiple cases can share the same code block:

Syntax

switch (expression) {
    case 1:
    case 2:
        // code
        break;
    case 3:
        // code
        break;
    default:
        // code
}
Example 1: Number Range

#include 
int main() {
    int number = 5;
    switch (number) {
        case 1 ... 10: printf("The number is between 1 and 10\n"); break;
        default: printf("The number is not between 1 and 10\n");
    }
    return 0;
}
Output

The number is between 1 and 10
Example 2: Character Type

#include 
int main() {
    char ch = 'g';
    switch (ch) {
        case 'a' ... 'z': printf("%c is a lowercase alphabet\n", ch); break;
        case 'A' ... 'Z': printf("%c is an uppercase alphabet\n", ch); break;
        case 48 ... 57: printf("%c is a digit\n", ch); break;
        default: printf("%c is a non-alphanumeric character\n", ch);
    }
    return 0;
}
Output

g is a lowercase alphabet