The Switch Statement


We have seen that when one of the many options is selected, we can use the if statement to control the selection. However, as the number of options increases, so does the complexity of this type of program. The program becomes difficult to read and follow. Many times it can even confuse the person designing it. C has a built-in multiway decision statement known as a switch. The switch statement checks the value of the given variable against the list of case value values and when a match is found, a block of statements related to this case is executed. The simple form of a switch statement is shown below:

switch(expression)
{
 case value1:
  //body
  break;
 case value1:
  //body
  break;
 default:
  //body
  break;
}
//body of code

The expression is an integer expression or characters. Value 1, value 2 .... known as constant or constant expression and case label. Each of these values should be unique in a switch statement. There is no need to put a brace around the "// body". Note that case labels end with a colon (:).

When the switch is executed, the value of the expression is successfully compared against the values value1, value2 ..... If a case is found whose value matches the value of the expression, the block of statements following the case is executed.

The break statement at the end of each block indicates the end of a particular case and exits the switch statement by following the switch and transferring the control to "//body of code".

The default is an optional case. While present, if the value of the expression does not match the values in any case, it will be executed. If not present, no action is taken if all matches fail and the control code is moved to "// body of code."

Selection process of the switch statement

Sample Program

Program
#include <stdio.h>

int main()
{
    int marks;
    printf("Enter your marks\n");
    scanf("%d",&marks);
    int ex=marks/10;
    switch(ex){
        case 2:
         printf("Grade : D");
         break;
        case 3:
         printf("Grade : C");
         break;
        case 4:
         printf("Grade : C+");
         break;
        case 5:
         printf("Grade : B");
         break;
        case 6:
         printf("Grade : B+");
         break;
        case 7:
         printf("Grade : A");
         break;
        case 8:
         printf("Grade : A+");
         break;
        case 9:
         printf("Grade : AA");
         break;
        default:
         printf("You entered wrong marks\n");
         break;
    }
    return 0;
}

Rules for switch statement

  1. Switch expression must be an integral type.
  2. Case labels must be constant or constant expressions.
  3. Case labels must be unique. No two labels can have the same value.
  4. Case labels must end with a colon.
  5. The break statement transfers control outside the switch statement.
  6. Break statement is optional. That means two or more case labels may be in the same statement.
  7. The default label is optional. If present, it will be applied in case the expression does not match.
  8. A maximum can have a default label.
  9. The default can be placed anywhere but is usually placed at the end.
  10. It is allowed in nested switch statements.

Sample Program

Program
#include <stdio.h>

int main()
{
    char ch;
    int a,b;
    printf(" a for Addition \n s for Subtraction \n m for Multiplication \n d for Division\n");
    scanf("%c",&ch);
    switch(ch)
    {
        case 'a':
         printf("Enter your numbers to Addition\n");
         scanf("%d%d",&a,&b);
         printf("a + b = %d",a+b);
         break;
        case 's':
         printf("Enter your numbers to Subtraction\n");
         scanf("%d%d",&a,&b);
         printf("a - b = %d",a-b);
         break;
        case 'm':
         printf("Enter your numbers to Multiplication\n");
         scanf("%d%d",&a,&b);
         printf("a * b = %d",a*b);
         break;
        case 'd':
         printf("Enter your numbers to Division\n");
         scanf("%d%d",&a,&b);
         printf("a / b = %d",a/b);
         break;
        default:
         printf("You choose wrong option \n");
         break;
    }
    return 0;
}