C Programs Tutorials | IT Developer
IT Developer

C Programming - switch statement in C



Share with a Friend

C Programming - switch statement in C

switch Statement in C

The switch statement in C is used to handle multiple possible conditions based on the value of a variable. Unlike a series of if-else statements, the switch statement provides a more efficient and readable way to handle multiple possible cases when the conditions are related to a single expression or variable.

Syntax of switch Statement:

C

switch (expression) {

    case value1:

        // Code to be executed if expression is equal to value1

        break;

    case value2:

        // Code to be executed if expression is equal to value2

        break;

    case value3:

        // Code to be executed if expression is equal to value3

        break;

    default:

        // Code to be executed if expression doesn't match any case

}

  • expression: This is the variable or expression whose value is compared against each case.
  • case valueN: If the expression matches valueN, the associated code block is executed.
  • break: This statement ends the switch block after a matching case is found and executed. Without break, the program will "fall through" and continue checking the next cases.
  • default: This is an optional case that is executed if none of the specified cases match the expression value. It acts like an "else" in if-else statements.

Example 1: Basic switch Statement

C

#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;

        case 6:

            printf("Saturday\n");

            break;

        case 7:

            printf("Sunday\n");

            break;

        default:

            printf("Invalid day\n");

    }

    return 0;

}

Explanation:

  • The variable day has the value 3.
  • The switch statement checks the value of day and matches it with case 3.
  • Since day is 3, it will print "Wednesday".
  • The break ensures that no further cases are checked once a match is found.

Output:

Wednesday

Example 2: switch Statement Without break (Fall-through Behavior)

C

#include <stdio.h>

int main() {

    int day = 2;

    switch (day) {

        case 1:

            printf("Monday\n");

            // No break here

        case 2:

            printf("Tuesday\n");

            // No break here

        case 3:

            printf("Wednesday\n");

            break;

        default:

            printf("Invalid day\n");

    }

    return 0;

}

Explanation:

  • The value of day is 2.
  • The switch matches case 2 and starts executing from there, but since there's no break after case 1, the program "falls through" and also executes case 2 and case 3.
  • The output will be:
    • "Tuesday"
    • "Wednesday"

Output:

Tuesday

Wednesday

Example 3: Using default Case

C

#include <stdio.h>

int main() {

    int month = 13;

    switch (month) {

        case 1:

            printf("January\n");

            break;

        case 2:

            printf("February\n");

            break;

        case 3:

            printf("March\n");

            break;

        case 4:

            printf("April\n");

            break;

        case 5:

            printf("May\n");

            break;

        case 6:

            printf("June\n");

            break;

        case 7:

            printf("July\n");

            break;

        case 8:

            printf("August\n");

            break;

        case 9:

            printf("September\n");

            break;

        case 10:

            printf("October\n");

            break;

        case 11:

            printf("November\n");

            break;

        case 12:

            printf("December\n");

            break;

        default:

            printf("Invalid month\n");

    }

    return 0;

}

Explanation:

  • The variable month has the value 13, which does not match any case in the switch statement.
  • Since no case matches, the default case is executed, printing "Invalid month".

Output:

Invalid month

Example 4: switch with char Data Type

C

#include <stdio.h>

int main() {

    char grade = 'B';

    switch (grade) {

        case 'A':

            printf("Excellent\n");

            break;

        case 'B':

            printf("Good\n");

            break;

        case 'C':

            printf("Average\n");

            break;

        case 'D':

            printf("Poor\n");

            break;

        default:

            printf("Invalid grade\n");

    }

    return 0;

}

Explanation:

  • The variable grade is 'B'.
  • The switch statement checks for a match with the case 'B', and since it matches, it prints "Good".

Output:

Good

Important Notes:

  1. Fall-through Behavior: In C, if there is no break statement after a case, the program will continue executing the following cases. This is called "fall-through" behavior. It is often used intentionally, but it can lead to logical errors if not carefully managed.
  2. Case Sensitivity: The switch statement is case-sensitive, meaning case 'A' is different from case 'a'.
  3. default Case: The default case is optional but recommended, especially when handling unexpected values.
  4. Expression Type: The expression inside the switch can be of integer types, char, or enum. However, it cannot be a floating-point number or a string.

Summary of switch Statement:

  • The switch statement is used for selecting one of many possible cases based on the value of an expression.
  • It is more efficient than using multiple if-else conditions when comparing a single variable with many possible values.
  • It works by matching the value of the expression to one of the case values, executing the corresponding block, and optionally using the break statement to stop further checks.
  • The default case is executed if no other case matches the expression value.