YourCodingMentor

The switch statement in C is used to execute one out of multiple possible blocks of code based on the value of an expression. It is an alternative to multiple if...else if...else conditions when you have several possible values to check for a variable.

Syntax

switch (expression) {
    case value1:
        // Code block for value1
        break;
    case value2:
        // Code block for value2
        break;
    case value3:
        // Code block for value3
        break;
    default:
        // Code block if none of the cases match
}
  • expression: The variable or expression that is evaluated once.
  • case value: Compares the expression with the value. If they match, the corresponding block of code is executed.
  • break: Exits the switch statement. Without break, the program will continue executing the next cases even if the current one has matched.
  • default: Executes the block if none of the cases match the expression. The default block is optional.

Example 1: Basic switch Statement

#include <stdio.h>

int main() {
    int number = 2;

    switch (number) {
        case 1:
            printf("You selected one.\n");
            break;
        case 2:
            printf("You selected two.\n");
            break;
        case 3:
            printf("You selected three.\n");
            break;
        default:
            printf("Invalid selection.\n");
    }

    return 0;
}

Output:

You selected two.

In this example, the value of number is 2, so the case 2 block is executed. The break statement ensures that the program exits the switch statement after executing the corresponding case.

Example 2: Without break (Fall-through Behavior)

If you omit the break statement, the program will “fall through” to the next case even if a match has been found. This can be useful in certain scenarios but should be used carefully.

#include <stdio.h>

int main() {
    int number = 2;

    switch (number) {
        case 1:
            printf("You selected one.\n");
        case 2:
            printf("You selected two.\n");
        case 3:
            printf("You selected three.\n");
        default:
            printf("Invalid selection.\n");
    }

    return 0;
}

Output:

You selected two.
You selected three.
Invalid selection.

Here, the program falls through the cases starting from case 2 because there are no break statements. Each subsequent case is executed until the default block is reached.

Example 3: Using default Case

The default case is executed when no case matches the value of the expression. It is optional but useful to handle unexpected values.

#include <stdio.h>

int main() {
    int number = 4;

    switch (number) {
        case 1:
            printf("You selected one.\n");
            break;
        case 2:
            printf("You selected two.\n");
            break;
        case 3:
            printf("You selected three.\n");
            break;
        default:
            printf("Invalid selection.\n");
    }

    return 0;
}

Output:

Invalid selection.

In this case, since number is 4 and there is no case 4, the default block is executed.

Example 4: Multiple case Labels for the Same Block

You can have multiple case labels that execute the same block of code. This can be useful when you want to handle multiple values in the same way.

#include <stdio.h>

int main() {
    int grade = 3;

    switch (grade) {
        case 1:
        case 2:
        case 3:
            printf("You are in the beginner level.\n");
            break;
        case 4:
        case 5:
            printf("You are in the intermediate level.\n");
            break;
        case 6:
            printf("You are in the advanced level.\n");
            break;
        default:
            printf("Invalid grade.\n");
    }

    return 0;
}

Output:

You are in the beginner level.

In this example, the grade 1, 2, or 3 will result in the same output, as they share the same block of code.

Example 5: Switch with Characters

You can also use switch with char values, and it works the same way as with integers.

#include <stdio.h>

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

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

    return 0;
}

Output:

Good job!

Important Notes

  1. Type of Expression: The expression in the switch statement can be an integer, character, or enumeration. It is not allowed to use floating-point numbers, strings, or other complex data types.
  2. Fall-through Behavior: If you do not use a break statement, the program will continue executing the next case, which is called fall-through. It is important to be mindful of this to avoid unintended behavior.
  3. default Case: The default case is optional but is often used to handle unexpected or out-of-range values. It is not required but helps in maintaining robustness.

Summary

  • The switch statement is used to simplify complex if...else if chains when you need to compare the same variable or expression against different values.
  • The expression in the switch statement must be an integer, character, or enumeration.
  • case: Each possible value is compared with the expression.
  • break: Prevents fall-through behavior after a match is found.
  • default: Handles cases where no match is found (optional).

Leave a Reply

Your email address will not be published. Required fields are marked *