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

C Switch Case Statement Syntax

The basic syntax of this 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 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 programming 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 of a switch statement in C

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

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

C switch Case Statement Output

OUTPUT 2: Let us enter the wrong operator to check the default value. Please refer to the If Else, 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 +, -, *, /, %

Can we use multiple conditions in a C switch case?

In a switch condition, we can use any expression, but in case labels, we cannot use multiple conditions. However, we can use the switch case with multiple values to perform the same action.

TIP: In a switch case, we must use a single constant value. It does not support expressions such as (x < 10, x > 10, x = 10, and so on). To perform these actions, use if else statements.

In the following example, we declared a char variable (ch) that represents the student’s grades. Next, for multiple case values, we are performing a single action. If a student’s grade is an A or a B gets the full scholarship.

#include <stdio.h>

int main()
{
char ch = 'B';

switch (ch)
{
case 'A':
case 'B':
printf("You will get the Scholarship\n");
break;
case 'C':
case 'D':
printf("Better luck Next time\n");
break;
default:
printf("Exam Fail\n");
}

return 0;
}
You will get the Scholarship

C switch case without a break statement

When working with the switch case, the break statement plays a vital row and we must use it for smoother operations. By default, the compiler starts executing the switch case from top to bottom.

When the case label satisfies the switch condition, it executes the case label code and moves to the next label. However, if we place a break statement, it stops the compiler from going to the next case and exits the compiler from a switch case block.

In the following example, we use the C switch case without a break statement. Here, case 1 satisfies the switch condition. However, the program prints all the information from two case labels and the default message.

#include <stdio.h>
int main()
{
int day = 1;
switch (day)
{
case 1:
printf("Monday\n");
case 2:
printf("Tuesday\n");
default:
printf("Not a valid Day\n");
}
return 0;
}
Monday
Tuesday
Not a valid Day

If we add the break statement to the above program, the result becomes Monday. Once the condition is met, the compiler executes the break statement and a compiler jumps out of the switch statement.

#include <stdio.h>
int main()
{
int day = 1;
switch (day)
{
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
default:
printf("Not a valid Day\n");
}
return 0;
}
Monday

C switch case vs else if statement

Both the switch case and the else if statement are control flow statements, but they serve different purposes.

  • If you are comparing a single constant value, a switch is the better choice. For example, display a message based on a student’s grades.
  • When there are logical operations (combining multiple expressions) at each level, use an else if or if-else statement.
  • Using an else if statement with multiple conditions is smarter than a switch case with multiple values (multiple case labels with the same value).
  • Using switch case on strings is not possible, whereas we can use the else if statement for string comparison (strcmp).

C switch case multiple values example

In the following example, we show a simple example to check whether the user-given day number is a weekday or a weekend. Here, we used the switch case where multiple case labels point to the same value (output).

#include <stdio.h>

int main()
{
int day = 7;

switch (day)
{
case 1:
case 2:
case 3:
case 4:
case 5:
printf("Weekday (Working day)\n");
break;

case 6:
case 7:
printf("Enjoy the Weekend\n");
break;

default:
printf("Enter a Valid day Number\n");
}

return 0;
}
Enjoy the Weekend

Else if example

It is better to use the else if statement in such a scenario. In the example below, each else if statement uses logical operators to evaluate multiple expressions.

If the day is between 1 and 5, it’s a weekday, and if it is either 6 or 7, it’s a weekend; otherwise, it’s an invalid day.

#include <stdio.h>

int main()
{
int day = 7;

if (day >= 1 && day <= 5)
{
printf("Weekday (Working day)\n");
}
else if (day == 6 || day == 7)
{
printf("Enjoy the Weekend\n");
}
else
{
printf("Enter a Valid day Number\n");
}

return 0;
}
Enjoy the Weekend