Switch Case in C

If Else in C allows us to choose between TRUE or FALSE, when there are more than two options, we use Nested If. Say, What if we have ten alternatives to choose?, if we use Nested If, in this situation, the programming logic will be difficult to understand. In C Programming, Else if statement and Switch statements can handle these types of problems effectively. We already discussed the Else If Statement in our previous post so, let us explore the switch case in C language here.

The working functionality of the switch case in C is almost the same as if condition. As we said before, the Switch statement may have n number of cases. So, the switch case compares the expression value with the values assigned in the case statements. If both the values expression value and case value match, then statements present in that case statement will execute. Let us see the syntax of the switch case in C Programming for better understanding

Syntax of Switch Case in C

The basic syntax of the Switch Case is as follows:

Switch (expression) 
{
 Case Option 1:
    //Execute these statements when the expression result match Option 1
    break;
 Case Option 2:
    //Execute these statements when the expression result match Option 2
    break;
 Case Option 3:
    //Execute these statements when the expression result match Option 3
    break;
    ......
 Case Option N:
    //Execute these statements when the result of expression match Option N
    break;
 Default:
    /*Execute these statements when the result of expression Not matching with any Option */
    break;
}

The value of expression should be either integer or characters (We can write the expression as n/2…. also, but the result should be an integer). Option 1, Option 2…… are constants.

C Switch case allows us to add a default statement. If the variable value is not matching with any of the case statements, the code present in the default will execute.

A break is useful to come out from the switch statement. Otherwise, all the statements in the C switch condition will execute. Whenever a Break Statement encountered, the execution flow would directly come out of the switch.

Flow Chart for Switch Case

Switch Case will only accept either integers or characters, whereas Else if statement takes decimal values (2.3, 3.5, etc.) also. The flow chart of the switch case in C is as shown below

Flow Chart for Switch Case in C Programming

Switch Case in C Example

This program allows the user to enter two integer values. Next, it also allows them to select any Arithmetic Operator to perform arithmetic operations using the Switch Case.

/* Switch Case in C Programming example */

#include<stdio.h>
int main()
{
 char opertor;
 int number1, number2;

 printf(" Please select any ARITHMETIC OPERATOR You wish!\n");
 scanf("%c",&opertor);

 printf("\n Please Enter two values to perform Arithmetic Operations\n");
 scanf("%d %d",&number1,&number2);

 switch (opertor)
 {
  case '+':
       printf("Addition of two numbers is: %d", number1 + number2);
       break;
  case '-':
       printf("Subtraction of two numbers is: %d", number1 - number2);
       break;
  case '*':
       printf("Multiplication of two numbers is: %d", number1 * number2);
       break;
  case '/':
       printf("Division of two numbers is: %d", number1/number2);
       break;
  case '%':
       printf("Module of two numbers is: %d",number1%number2);
       break;
  default:
       printf("You have entered Wrong operator\n");
       printf("Please enter the Correct operator such as +, -, *, /, %%");
       break;
 }
 return 0;
}

For the demonstration purpose, let us enter * operator and enter number1 = 10, number 2 =8.

Switch Case in C Programming Output 1

OUTPUT 2: Let us enter the wrong operator to check the default value. Please refer to If Else in C, Nested If, and Else If Statement articles.

 Please select any ARITHMETIC OPERATOR You wish!
@

 Please Enter two values to perform Arithmetic Operations
3
4
You have entered Wrong operator
Please enter the Correct operator such as +, -, *, /, %