C switch Statement -

C switch Statement

The switch statement in C is a control structure that simplifies decision-making by evaluating a single expression and executing the matching case. Unlike if else, the switch is designed for situations where you need to compare one variable against multiple constant values.


What Is the switch Statement?

The switch statement evaluates an expression and executes the block of code corresponding to the first matching case. If no case matches, the default case is executed, if provided.


Syntax of switch

switch (expression) {
    case constant1:
        // Code to execute if expression equals constant1
        break;
    case constant2:
        // Code to execute if expression equals constant2
        break;
    // Add more cases as needed
    default:
        // Code to execute if no case matches
}

Key Components

  1. switch (expression):
    Evaluates the expression. The result is compared to each case.
  2. case constant:
    If the expression matches this constant, the associated block is executed.
  3. break:
    Exits the switch block. Without break, execution continues to the next case (fall-through behavior).
  4. default:
    Optional. Executes if no case matches.

Example 1: Basic switch Statement

#include <stdio.h>

int main() {
    int day = 3;

    switch (day) {
        case 1:
            printf("Monday\n");
            break;
        case 2:
            printf("Tuesday\n");
            break;
        case 3:
            printf("Wednesday\n");
            break;
        case 4:
            printf("Thursday\n");
            break;
        case 5:
            printf("Friday\n");
            break;
        default:
            printf("Weekend\n");
    }

    return 0;
}

Output:

Wednesday

Real-Life Examples

Example 2: Grade Evaluation

#include <stdio.h>

int main() {
    char grade = 'B';

    switch (grade) {
        case 'A':
            printf("Excellent!\n");
            break;
        case 'B':
            printf("Well done!\n");
            break;
        case 'C':
            printf("Good work!\n");
            break;
        case 'D':
            printf("Keep trying.\n");
            break;
        default:
            printf("Invalid grade.\n");
    }

    return 0;
}

Fall-Through Behavior

By default, if a break statement is not used, execution will continue to the next case.

Example 3: Fall-Through

#include <stdio.h>

int main() {
    int number = 2;

    switch (number) {
        case 1:
            printf("One\n");
        case 2:
            printf("Two\n");
        case 3:
            printf("Three\n");
            break;
        default:
            printf("Other\n");
    }

    return 0;
}

Output:

Two
Three

Adding break to Prevent Fall-Through

To avoid unintended fall-through:

#include <stdio.h>

int main() {
    int number = 2;

    switch (number) {
        case 1:
            printf("One\n");
            break;
        case 2:
            printf("Two\n");
            break;
        case 3:
            printf("Three\n");
            break;
        default:
            printf("Other\n");
    }

    return 0;
}

Output:

Two

default Case

The default case ensures that a statement is executed when no matching case is found.


Real-Life Example: Simple Calculator

#include <stdio.h>

int main() {
    char operator;
    double num1, num2;

    printf("Enter an operator (+, -, *, /): ");
    scanf("%c", &operator);

    printf("Enter two numbers: ");
    scanf("%lf %lf", &num1, &num2);

    switch (operator) {
        case '+':
            printf("%.2lf + %.2lf = %.2lf\n", num1, num2, num1 + num2);
            break;
        case '-':
            printf("%.2lf - %.2lf = %.2lf\n", num1, num2, num1 - num2);
            break;
        case '*':
            printf("%.2lf * %.2lf = %.2lf\n", num1, num2, num1 * num2);
            break;
        case '/':
            if (num2 != 0) {
                printf("%.2lf / %.2lf = %.2lf\n", num1, num2, num1 / num2);
            } else {
                printf("Division by zero is not allowed.\n");
            }
            break;
        default:
            printf("Invalid operator.\n");
    }

    return 0;
}

Nested switch Statement

You can place one switch inside another for more complex decision-making.

Example 5: Nested switch

#include <stdio.h>

int main() {
    int choice1 = 1, choice2 = 2;

    switch (choice1) {
        case 1:
            printf("Outer switch: Choice 1\n");

            switch (choice2) {
                case 1:
                    printf("Inner switch: Choice 1\n");
                    break;
                case 2:
                    printf("Inner switch: Choice 2\n");
                    break;
            }
            break;
        case 2:
            printf("Outer switch: Choice 2\n");
            break;
    }

    return 0;
}

Why Use switch?

  • Readable: Easier to follow than a chain of if else.
  • Efficient: Compiler optimizes switch for faster execution.
  • Specific Use: Ideal when testing a single variable against multiple values.

For more detailed content, visit Master Coding Science at mastercodingscience.com.

Leave a Comment