Switch Case in C

Switch Case in C: The If Else 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 from? if we use Nested If, the programming logic will be difficult to understand in this situation.

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 here.

The working functionality of the switch case in C is almost the same as if condition. As we said before, the C Switch statement may have n number of cases. So, it 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 for a better understanding.

Syntax of Switch Case in C

The basic syntax of this C Switch Case statement as follows:

Switch (expression) 
{
 Case Option 1:
    //Execute 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 when the expression result match Option 3
    break;
    ......
 Case Option N:
    //Execute when the result of expression match Option N
    break;
 Default:
    /*Execute when the result of expression Not matching with any Option */
    break;
}

The value of the expression should be either an 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.

The Switch statement in c 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 statement is useful to come out from the C switch case. Otherwise, all the statements in the condition will execute. Whenever a Break Statement is encountered, the execution flow would directly come out of it.

Flow Chart

It 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 C switch statement or case is as shown below

Switch Case Statement Flow Chart

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.

#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 the * 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 the 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 +, -, *, /, %