switch…case statement in C

if...else statement provides support to control program flow. if statement make decisions based on conditions. It selects an action, if some condition is met. However, there exits situations where you want to make a decision from available choices. For example – select a laptop from available models, select a menu from available menu list etc.

switch...case statement gives ability to make decisions from fixed available choices. Rather making decision based on conditions. Using switch we can write a more clean and optimal code, that take decisions from available choices.

Syntax of switch...case statement

switch(expression)
{
    case 1: 
        /* Statement/s */
        break;
    case 2: 
        /* Statement/s */
        break;
    case n: 
        /* Statement/s */
        break;
    default: 
        /* Statement/s */
}

Rules for working with switch case

  • Expression inside switch must evaluate to integer, character or enumeration constant. switch...case only works with integral, character or enumeration constant.
  • The case keyword must follow one constant of type evaluated by expression. The case along with a constant value is known as switch label.
  • You can have any number of cases.
  • Each and every case must be distinct from other. For example, it is illegal to write two case 1 label.
  • You are free to put cases in any order. However, it is recommended to put them in ascending order. It increases program readability.
  • You can have any number of statement for a specific case.
  • The break statement is optional. It transfers program flow outside of switch...case. break statement is covered separately in this C tutorial series.

    Read more about – break statement in C.

  • The default case is optional. It works like an else block. If no cases are matched then the control is transferred to default block.

Working of switch...case statement

Let me take an example to demonstrate the working of switch...case statement.

int num = 2;

switch(num)
{
    case 1: printf("I am One");
        break;
    case 2: printf("I am Two"); 
        break;
    case 3: printf("I am Three");
        break;
    default: printf("I am an integer. But, definitely I am not 1, 2 and 3.");
}
  • Initially I declared an integer variable num = 2.
  • switch(num) will evaluate the value of num to 2.
  • After switch(num) got evaluated, switch knows the case to transfer program control.
  • Instead of checking all cases one by one. It transfers program control directly to case 2. If value of num is not matched with any case then switch transfers control to default case, if defined.
  • After the control has been set to case 2. It executes all statements inside the case. The case contains two statement first printf("I am Two"); and second break. printf("I am Two"); will print “I am Two” on console and transfers control to break.
  • break statement terminates switch...case and transfer program control to statement after switch.

    What if I don’t use break keyword? If you don’t use break keyword, it executes all below cases until break statement is found. It doesn’t matter whether the remaining case matches or not, it will execute all below case from matching case in the absence of break keyword.

Flowchart of switch...case statement

switch...case statement flowchart

Example program of switch...case statement

Let us write a C program to input week number from user and print the corresponding day name of week.

/**
 * C program to print day of week name
 */

#include <stdio.h>

int main()
{
    /* Declare integer variable to store week number */
    int week;

    /* Input week number from user */
    printf("Enter week number (1-7): ");
    scanf("%d", &week);

    switch(week)
    {
        case 1: 
            /* If week == 1 */
            printf("Its Monday.\n");
            printf("Its a busy day.");
            break;
        case 2: 
            /* If week == 2 */
            printf("Its Tuesday.");
            break;
        case 3: 
            /* If week == 3 */
            printf("Its Wednesday.");
            break;
        case 4: 
            /* If week == 4 */
            printf("Its Thursday.\n");
            printf("Feeling bit relaxed.");
            break;
        case 5: 
            /* If week == 5 */
            printf("Its Friday.");
            break;
        case 6: 
            /* If week == 6 */
            printf("Its Saturday.\n");
            printf("It is weekend.");
            break;
        case 7: 
            /* If week == 7 */
            printf("Its Sunday.\n");
            printf("Hurray! Its holiday.");
            break;
        default: 
            /* If week < 1 or week > 7 */
            printf("Um! Please enter week number between 1-7.");
    }
    return 0;
}

Practice exercises – Switch case programming exercises in C.

Output –

Enter week number (1-7): 6
Its Saturday.
It is weekend.

Nesting of switch...case statement

C supports nesting of one switch...case inside other. However, it is not recommended model to put one switch...case inside other. As nesting of switch decreases code readability.

Syntax of nested switch...case statement

switch(expression)
{
    case 1: 
        /* Statement/s */
        break;
    case 2: 
        /* Statement/s */
        switch(inner_expression)
        {
            case 1: 
                /* Statement/s */
                break; 
            case 2: 
                /* Statement/s */
                break; 
            case n: 
                /* Statement/s */
                break; 
            default: 
                /* Statement/s */
                break;
        }
        break;
    case n:
        /* Statement/s */
        break;
    default: /* Default statement/s */
}