C Programs Tutorials | IT Developer
IT Developer

C Programming - nested switch statement in C



Share with a Friend

C Programming - nested switch statement in C

Nested switch Statement in C

A nested switch statement in C refers to placing one switch statement inside another switch statement. This allows for checking multiple conditions at different levels and is useful when you have complex decision-making requirements.

The inner switch statement will only be evaluated if the condition in the outer switch statement matches. Just like the regular switch statement, each switch block can have case, break, and default statements.

Syntax of Nested switch Statement:

C

switch (expression1) {

    case value1:

        // Code for case value1

        switch (expression2) {

            case value2:

                // Code for case value2

                break;

            case value3:

                // Code for case value3

                break;

            default:

                // Code if expression2 doesn't match any case

        }

        break;

    case value4:

        // Code for case value4

        break;

    default:

        // Code if expression1 doesn't match any case

}

  • The outer switch checks expression1.
  • If a case is matched, the inner switch checks expression2.
  • The inner switch executes based on expression2's value if the outer switch condition is true.

Example: Nested switch Statement

C

#include <stdio.h>

int main() {

    int outer = 2, inner = 3;

    switch (outer) {

        case 1:

            printf("Outer case 1\n");

            break;

        case 2:

            printf("Outer case 2\n");

            switch (inner) {

                case 1:

                    printf("Inner case 1\n");

                    break;

                case 2:

                    printf("Inner case 2\n");

                    break;

                case 3:

                    printf("Inner case 3\n");

                    break;

                default:

                    printf("Inner default case\n");

            }

            break;

        case 3:

            printf("Outer case 3\n");

            break;

        default:

            printf("Outer default case\n");

    }

    return 0;

}

Explanation:

  • The outer switch checks if the outer variable is 1, 2, or 3.
  • In this case, outer is 2, so it matches case 2.
  • Inside the outer case 2, a second switch is executed, which checks the value of the inner variable.
  • Since inner is 3, it matches case 3 in the inner switch, and "Inner case 3" is printed.

Output:

Outer case 2

Inner case 3

Example 2: More Complex Nested switch

C

#include <stdio.h>

int main() {

    int outer = 3, inner = 1;

    switch (outer) {

        case 1:

            printf("Outer case 1\n");

            break;

        case 2:

            printf("Outer case 2\n");

            switch (inner) {

                case 1:

                    printf("Inner case 1\n");

                    break;

                case 2:

                    printf("Inner case 2\n");

                    break;

                default:

                    printf("Inner default case\n");

            }

            break;

        case 3:

            printf("Outer case 3\n");

            switch (inner) {

                case 1:

                    printf("Inner case 1\n");

                    break;

                case 2:

                    printf("Inner case 2\n");

                    break;

                default:

                    printf("Inner default case\n");

            }

            break;

        default:

            printf("Outer default case\n");

    }

    return 0;

}

Explanation:

  • The outer switch checks if outer is 1, 2, or 3.
  • Since outer is 3, the program enters case 3.
  • Inside this, another switch is executed for the inner variable.
  • Since inner is 1, it matches case 1 in the inner switch, and "Inner case 1" is printed.

Output:

Outer case 3

Inner case 1

Key Points about Nested switch Statements:

  1. Nested Structure: A switch can be nested inside another switch, making it useful for situations where decisions depend on multiple variables.
  2. Control Flow: The inner switch only gets evaluated when the corresponding outer switch case is matched.
  3. Case Matching: If multiple switch cases match, the code block associated with the first matching case is executed. All the following cases are skipped unless break is not used.
  4. Fall-through: Just like a regular switch, if there is no break statement, the program will continue executing the next case statements. However, this is rarely used in nested switch statements.
  5. Default Case: Both the outer and inner switch blocks can have a default case, which is executed if no other cases match.

Summary:

  • A nested switch allows multiple layers of decision-making based on different conditions.
  • The outer switch checks the first condition, and if it matches, it checks for inner conditions through another switch.
  • This structure is helpful for handling complex decision-making processes with multiple related conditions.