switch case statement in C programming

switch case is a multiple branching statement which compares the value of expression or variable inside switch() with various cases provided with the statement and executes a block when a match is found. If no cases inside the switch is matched, the statements inside default block is executed. However, default is optional and may not be present. It is similar to else part of if statement.

switch case can be considered as simplified version of if statement. When there are large number of conditions to be tested, it is difficult to use if statement as the number of repeated if statements may cause confusion and makes the program unreadable. So, switch case is preferred in such cases to simplify programmers job and increases code readability.

Syntax of switch...case statement

switch (variable or expression)
{
    case value1:
        statement(s);
        break;
    case value2:
        statement(s);
        break;
    ... ... ...
    ... ... ...
    case valueN:
        statement(s);
        break;
    default:
        statement(s);
        break;
}

Flowchart of switch case statement

switch case in c programming

Example of switch case statement

Example 1: Check if entered alphabet is vowel or a consonant. (Incorrect output)

#include <stdio.h>
int main()
{
    char alphabet;
    printf("Enter an alphabet:");
    scanf("%c",&alphabet);
    switch(alphabet)
    {
        case 'a':
            printf("Alphabet a is a vowel.\n");
        case 'e':
            printf("Alphabet e is a vowel.\n");
        case 'i':
            printf("Alphabet i is a vowel.\n");
        case 'o':
            printf("Alphabet o is a vowel.\n");
        case 'u':
            printf("Alphabet u is a vowel.\n");
        default:
            printf("You entered a consonant.\n");
    }
    return 0;
}

Output

Enter an alphabet:i
Alphabet i is a vowel.
Alphabet o is a vowel.
Alphabet u is a vowel.
You entered a consonant.

In this program, an alphabet is stored in a variable alphabet. Using a switch case statement, we check for all the cases a, e, i, o, u and statement inside the case is executed. But when users enters alphabet i, the program prints all statements following the matching case, i.e. case i. This is because, switch case, by design, executes all statements after the match is found until the end of the block. To get around this, we need to break its execution. This is done using break statement.

break Statement

The break statement is used to break out of a loop or a switch case. It is very important to use break statement inside switch case, where when a matching case is found, all the cases below it are executed by default. So, break statement is used after each case in order to break out of switch..case after a case has been matched.

Syntax of break Statement

break;

Example of switch case statement using break statement


Example 2: Check if entered alphabet is vowel or a consonant. (Correct output)

#include <stdio.h>
int main()
{
    char alphabet;
    printf("Enter an alphabet:");
    scanf("%c",&alphabet);
    switch(alphabet)
    {
        case 'a':
            printf("Alphabet a is a vowel.\n");
            break;
        case 'e':
            printf("Alphabet e is a vowel.\n");
            break;
        case 'i':
            printf("Alphabet i is a vowel.\n");
            break;
        case 'o':
            printf("Alphabet o is a vowel.\n");
            break;
        case 'u':
            printf("Alphabet u is a vowel.\n");
            break;
        default:
            printf("You entered a consonant.\n");
            break;
    }
    return 0;
}

Output

Enter an alphabet:i
Alphabet i is a vowel.

Above examples illustrates the use of of break statement. In each program, user enters a number which is then compared with various cases inside switch statement. In the first example, break statement is not used so if a matching case is found, all the statements below it are executed. However in the second example, break is used so only the statements inside the matching case is executed.

Nested switch case

Like nested if, we can use nested switch case in C programming. A switch case statement enclosed inside another switch case statement is called nested switch case.

Syntax of nested switch case

switch (variable or expression)
{
    case value1:
        statement(s);
        switch (variable or expression)
        {
            [body of nested switch]
        }
        break;
    ... ... ...
    ... ... ...
    case valueN:
        statement(s);
        switch (variable or expression)
        {
            [body of nested switch]
        }
        break;
    default:
        statement(s);         
        switch (variable or expression)
        {
            [body of nested switch]
        }
        break;
}

Example of nested switch case statement

Example 3: C program to check for head/tail using nested switch case statement 

#include<stdio.h>
int main()
{
    int ch1,ch2;
    printf("H/h for head, T/t for tail\n");
    printf("Enter first choice-");
    scanf("%c",&ch1);
    fflush(stdin);
    printf("Enter second choice-");
    scanf("%c",&ch2);
    switch(ch1)
    {
        case 'h':
        case 'H':
            switch(ch2)
            {
                case 'h':
                case 'H':
                    printf("2 Heads");
                    break;
                default:
                    printf("Head and Tail");
            }
            break;
        default:
            switch(ch2)
            {
                case 'h':
                case 'H':
                    printf("Tail and Head");
                    break;
                default:
                    printf("2 Tails");
            }
    }
    return 0;
}

This program is an example of nested switch case. Here, a switch case is inserted inside another switch case. User needs to enter two characters, H/h for head and T/t for tail. Both switch case (outer and inner) tests whether the input entered by user is Head or Tail. According to the combination of inputs entered by user, the output is displayed.

Output

H/h for head, T/t for tail
Enter first choice-h
Enter second choice-t
Head and Tail
H/h for head, T/t for tail
Enter first choice-t
Enter second choice-t
2 Tails

Note: Nested switch aren't popularly used by programmers. In practice, generally nested if statements are preferred instead of nested switch case.