Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

The switch statement

In C programming, the switch statement is used to make a specific selection from multiple cases.

Syntax

switch(expression)
{
    case constant_value_1 :
                              statements to be executed;
                              break;
    case constant_value_2 :
                              statements to be executed;
                              break;
    case constant_value_3 :
                              statements to be executed;
            |                 break;
            |
            |

    case constant_value_N :
                              statements to be executed;
                              break;
    case default :
                              statements to be executed;
}

In a switch statement, the expression inside switch’s left & right parenthesis is executed first.
If any case matches the expression value, program control jumps directly to that case and statements of that case are executed.
If expression value does not match with any case, then program control jumps directly to the default case and statements of default case are executed.

Note

Constant values of cases must be integer or character value only, as well as they must be different. Otherwise, the compiler will show error.
Use of break statement is optional.

Flowchart

C Programming Language switch statement Flowchart

Switch statement working demo

C Programming Language switch statement Working
Example No 1

#include <stdio.h>
int main()
{
    int num;
    printf("\n Enter any number = ");
    scanf("%d",&num);
    switch(num)
    {
        case 10:
                printf("\n Inside case 10");
        case 20:
                printf("\n Inside case 20");
        case 30:
                printf("\n Inside case 30");
        case 40:
                printf("\n Inside case 40");
        case 50:
                printf("\n Inside case 50");

        default :
                printf("\n Inside default case");
    }
    return 0;
}

Output

Enter any number = 20

Inside case 20
Inside case 30
Inside case 40
Inside case 50
Inside default case

Here we have entered number 20, so it matches case 20. Program control enters into case 20 skipping unmatched case 10.

But when the program enters case 20 and executes it, all the cases below it including default case are also executed because we have not written break statement inside all the cases.

If we don’t want other cases to execute after the selected case, we should use a break statement in each case at the end, as shown below:

Example No 2

#include <stdio.h>
int main()
{
    int num;
    printf("\n Enter any number = ");
    scanf("%d",&num);
    switch(num)
    {
        case 10:
               printf("\n Inside case 10");
               break;
        case 20:
               printf("\n Inside case 20");
               break;
        case 30:
               printf("\n Inside case 30");
               break;
        case 40:
               printf("\n Inside case 40");
               break;
        case 50:
               printf("\n Inside case 50");
               break;
        default :
               printf("\n Inside default case");
    }
      printf("\n After switch statement");
      return 0;
}

Output

Enter any number = 20
Inside case 20
After switch statement

Example No 3

#include <stdio.h>
int main()
{
    char ch;
    printf("\n Enter any character = ");
    scanf("%c",&ch);
    switch(ch)
    {
        case 'A':
                 printf("\n Inside case A");
                 break;
        case 'B':
                 printf("\n Inside case B");
                 break;
        case 'C':
                 printf("\n Inside case C");
                 break;
        default :
                 printf("\n Inside default case");
    }
    return 0;
}

Output

Enter any character = B

Inside case B

Note

In a switch statement, default case can be used in any order.

 

Leave a reply