C Programming Tutorial

C Programming Tutorial

Nested Switch Case in C Programming

Hello Everyone, In this article, we will cover the concept of Nested Switch Case in C programming. We will cover how to implement and utilize nested switch statements to handle more complex decision-making scenarios within your code. Whether you’re new to C or have some experience, this guide will help you to understand Nested Switch Statement effectively. Let’s get started.

A nested switch in the C programming is a construct where one switch statement is contained within another. The inner switch statement is executed based on the value of a case label in the outer switch statement.

Here is an example:

switch (x) {
    case 1:
        switch (y) {
            case 2:
                // Do something
                break;
            case 3:
                // Do something else
                break;
            default:
                // Do something else entirely
                break;
        }
        break;
    case 4:
        // Do something completely different
        break;
    default:
        // Do something if neither case is matched
        break;
}

In this example, if the value of x is 1, the inner switch statement is executed based on the value of y. If y is 2, then the first case label is matched and the corresponding code is executed. If y is 3, the second case label is matched and its corresponding code is executed. If y does not match either case label, the default case is executed.

If the value of x is 4, the first case label in the outer switch statement is not matched, so the code after the case 4: label is executed. If the value of x does not match any of the case labels in the outer switch statement, the default case is executed.

Nested switch statements can be useful when you need to make multiple comparisons based on different variables. However, they can also make code harder to read and debug, so it’s important to use them judiciously.

Simple C program that uses a nested switch statement:

#include <stdio.h>
int main() {
    int x = 1;
    int y = 2;

    switch (x) {
        case 1:
            switch (y) {
                case 2:
                    printf("x is 1 and y is 2\\n");
                    break;
                case 3:
                    printf("x is 1 and y is 3\\n");
                    break;
                default:
                    printf("x is 1 and y is not 2 or 3\\n");
                    break;
            }
            break;
        case 4:
            printf("x is 4\\n");
            break;
        default:
            printf("x is neither 1 nor 4\\n");
            break;
    }

    return 0;
}

In this program, we declare two integer variables x and y, and initialize them to 1 and 2, respectively. We then use a nested switch statement to check the values of x and y.

If x is 1 and y is 2, we print the message “x is 1 and y is 2”. If x is 1 and y is 3, we print the message “x is 1 and y is 3”. If x is 1 but y is neither 2 nor 3, we print the message “x is 1 and y is not 2 or 3”. If x is 4, we print the message “x is 4”. If x is neither 1 nor 4, we print the message “x is neither 1 nor 4”.

Note that each switch statement is terminated with a break statement, which is necessary to prevent the program from falling through to the next case label.

Complex C program that uses nested switch statements to perform calculations based on user input:

#include <stdio.h>
int main() {
    int operation, value1, value2, result;

    printf("Enter operation code (1 = add, 2 = subtract, 3 = multiply): ");
    scanf("%d", &operation);

    printf("Enter first value: ");
    scanf("%d", &value1);

    printf("Enter second value: ");
    scanf("%d", &value2);

    switch (operation) {
        case 1:
            switch (value1) {
                case 1:
                    switch (value2) {
                        case 1:
                            result = 2;
                            break;
                        case 2:
                            result = 3;
                            break;
                        case 3:
                            result = 4;
                            break;
                        default:
                            printf("Invalid value for second number\\n");
                            return 1;
                    }
                    break;
                case 2:
                    switch (value2) {
                        case 1:
                            result = 3;
                            break;
                        case 2:
                            result = 4;
                            break;
                        case 3:
                            result = 5;
                            break;
                        default:
                            printf("Invalid value for second number\\n");
                            return 1;
                    }
                    break;
                case 3:
                    switch (value2) {
                        case 1:
                            result = 4;
                            break;
                        case 2:
                            result = 5;
                            break;
                        case 3:
                            result = 6;
                            break;
                        default:
                            printf("Invalid value for second number\\n");
                            return 1;
                    }
                    break;
                default:
                    printf("Invalid value for first number\\n");
                    return 1;
            }
            printf("The result is: %d\\n", result);
            break;
        case 2:
            result = value1 - value2;
            printf("The result is: %d\\n", result);
            break;
        case 3:
            result = value1 * value2;
            printf("The result is: %d\\n", result);
            break;
        default:
            printf("Invalid operation code\\n");
            return 1;
    }

    return 0;
}

This program prompts the user to enter an operation code (1 for addition, 2 for subtraction, or 3 for multiplication), as well as two integer values to perform the operation on. The program then uses nested switch statements to perform the operation and print the result.

If the operation code is 1 (addition), the program first checks the value of the first integer. If it’s 1, the program checks the value of the second integer and performs the appropriate addition operation. If the first integer is 2 or 3, the program performs the appropriate addition operation based on the value of the second integer. If either integer is an invalid value, the program prints an error message and exits with a status of 1.

If the operation code is 2 (subtraction), the program simply subtracts the second integer from the first integer and prints the result.

If the operation code is 3 (multiplication), the program simply multiplies the two integers together and prints the result.

If the operation code is an invalid value, the program prints an error message and exits with a status of 1.

Note that the use of nested switch statements in this program allows for complex logic to be executed based on the values of multiple variables. However, it can also make the code more difficult to read and maintain, so it’s important to use nested switch statements judiciously.

Share This Article