Switch Statements in C Language

The switch statement allows us to select one option from multiple code blocks to execute. In this tutorial, we will learn about switch statement in C programming language with the help of multiple examples. It is an alternative of if-else-if ladder. The syntax of the switch statement, on the other hand, is much easier to read and write.


Switch case statements are the substitutes of multiple if-else statements. The condition of a switch statement is a value, which can either be an integer or character. The syntax of switch statement is as following:

1. Switch Case Syntax in C:

C
switch (expression)​ {
  case value1:
    //code to be executed;
    break;

  case value2:
    // code to be executed;
    break;

    .....

  default:
    //code to be executed if all cases are not matched;
}

Here case says that if the value of expression is equal to the case value, then execute the corresponding statements in front of case statement. Break is a keyword that breaks out of the code block execution in a loop or switch statements. It is always a good practice to use break statement at the end of every case code blocks.

Whenever the control reaches to a break statement, it stops evaluation of the cases below it. In case if break statement is not applied, program will continuously evaluate further statements and at the last default block also will be executed irrespective of any case condition matched or not.

Switch Statement In C




2. Rules for switch statement:


  • An integer or character type must be used for the switch expression.
  • The case value must be a character constant or an integer.
  • The switch statement is the only place where the case value may be utilised.
  • The default statement is not necessary. The switch case statement would function normally even if it lacked a default statement.
  • The break statement is not necessary. Execution will move on to the following case if it is left out. Until a break is achieved, the control will pass through to future cases.


Example: Simple Calculator Program using Switch statement

C

#include <stdio.h>

int main() {
    char operation;
    double num1, num2;

    printf("Enter an operator (+, -, *, /): ");
    scanf("%c", &operation);
    printf("Enter first number: ");
    scanf("%lf", &num1);
    printf("Enter second number: ");
    scanf("%lf", &num2);

    switch(operation)
    {
        case '+':
            printf("%.1lf + %.1lf = %.1lf",num1, num2, num1+num2);
            break;

        case '-':
            printf("%.1lf - %.1lf = %.1lf",num1, num2, num1-num2);
            break;

        case '*':
            printf("%.1lf * %.1lf = %.1lf",num1, num2, num1*num2);
            break;

        case '/':
            printf("%.1lf / %.1lf = %.1lf",num1, num2, num1/num2);
            break;

        // if input doesn't match any case constant +, -, *, /
        default:
            printf("Error! operator is not correct");
    }

    return 0;
}

Output :

Enter an operator (+, -, *, /): / Enter first number: 10 Enter second number: 2 10.0 / 2.0 = 5.0