C Language Switch Case with Examples

Introduction

The switch case in c language is an alternative to if-else ladder. What is the meaning of if-else ladder? See the following example.

int i;
if(i==0)
printf("Zero");
else if(i==1)
printf("One");
else if(i==2)
printf("Two");
else if(i==3)
printf("Three");
else if(i==4)
printf("Four");
else
printf("Value of i is more than 4");

In the above example, I have written only a small part of the c program. There are so many if-else statements which compare the value of i with different values like 1, 2, 3 and 4. If any values from 1, 2, 3 and 4 matches with the value of i then respective printf() statement will be executed, otherwise the last else statement will be executed.

Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle

If there are more if-else statements, then the code becomes so complex. It becomes very difficult to read this code. So, here comes the concept of switch case in c language.

What is switch case in c language?

As I said above, it is an alternative to an if-else ladder. In other words, we can say, it is a multiway decision statement. Using a switch case in c language, we can perform various actions on the based on the evaluation of the single expression.

Syntax of switch case in c language

switch(expression)
{
  case value-1:
  block-1;
  break;
  case value-2:
  block-2;
  break;
  case value-3:
  block-3;
  break;
  .........
  .........
  .........
  case value-n:
  block-n;
  break;
  default:
  default-block;
}

From the above syntax, the switch statement has the value of a given variable or expression against a list of case values. The value of the expression is compared with the value of each case. If the match is found, then that particular block of that case will be executed.

Also Read: Insert and Delete element in Array in C using switch case

Here, the expression is of integer or character type only. The value-1, value-2,…..,value-N are integer or character constant or expression. These can be called as case labels followed by colons(:). These case labels must be unique. That means, we can not use the same case labels twice. In every case, there is a block of statements. This block may be empty.

For example,

int i=2;
switch(i)
{
  case 1:
  printf("This is case 1");
  break;
  case 2:
  printf("This is case 2");
  break;
  case 3:
  printf("This is case 3");
}

Here, the value of i is 2. The switch(i) that means that the value of i will be compared with case 1 first. But it is not matched, then it will go to case 2 and the case is matched. Therefore, the message, This is case 2 will be displayed on the console.

Also Read: C Program to Print Multiples of 5 using do while loop

Once we got our case and the block, then we should not go further. That means we should not execute further cases. For this purpose, we can use a keyword, break. This break keyword terminates the switch case. That means control should be transferred outside the switch case statement. Here, case 3 will not be executed. This is not compulsory to write the break statement inside the case.

C switch case without using break keyword

int i=2;
switch(i)
{
  case 1:
  printf("This is case 1");
  case 2:
  printf("This is case 2");
  case 3:
  printf("This is case 3");
}

In this example, I have not written break here. We know, case 2 will be executed. But there is no break statement. Therefore, case 3 will also be executed.

I hope you understood the difference. If there is no break, that means control will not come outside the switch statement until we reach the end of the switch statement.

While using switch case in c language, writing break is not compulsory.

Also Read: Operators in C with Detailed Explanation

What is C case default?

One more thing is there in the syntax. We have some limitations to use a number of cases. There are some occasions when user enters a choice or option other than the cases written inside the switch. Take the same example. If the value of i becomes 6, then what will happen? There will no output. But for such type of situation, we can use the keyword default. This is just like else statement in the if-else statement. Similarly, if no case has the value of that expression, then the default block will be executed.

Use of default keyword in c switch case

int i=6;
switch(i)
{
  case 1:
  printf("This is case 1");
  break;
  case 2:
  printf("This is case 2");
  break;
  case 3:
  printf("This is case 3");
  break;
  default:
  printf("This is default");
}

Here, the value of i is 6. No case has label or value 6. Therefore, we get the output “this is default“.

Important Points of switch case in c language

  1. The switch expression must be an integral type.
  2. Case labels must be constants or constant expression.
  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 the control out of the switch statement and it is optional.
  6. The default statement is optional. This is just like else statement in an if-else statement.
  7. You can use only one default statement at most and write anywhere in the switch statement.
  8. You can also use the switch case inside the switch statement in the c programming.

Some Important Examples of switch case in c Language

Find the output of the following program.

int main()
{
  int flag=1;
  switch(flag)
  {
    case 1: 
    printf("This is case 1\n");
    case 2: 
    printf("This is case 2\n");
    default: 
    printf("This is default\n");
  }
 return 0;
}

Output:

This is case 1
This is case 2
This is default

Why we got this output? In the above example, we have not used the break statement in the c switch case.

Also Read: Floyd Triangle in C Programming

Find the output of the following program.

int main()
{
  char C='A';
  switch(C)
  {
    case 'x': 
    printf("x");
    case 'y': 
    printf("y");
    default: 
    printf("other");
  }
 return 0;
}

Output:

other

Write a c program to display the names of the days of a week using switch case statement.

int main()
{
  int a;
  printf("Enter the number between 1 to 7 : ");
  scanf("%d",&a);
  switch(a)
  {
    case 1: 
    printf("Monday");
    break;
    case 2: 
    printf("Tuesday");
    break;
    case 3: 
    printf("Wednesday");
    break;
    case 4: 
    printf("Thursday");
    break;
    case 5: 
    printf("Friday");
    break;
    case 6: 
    printf("Saturday");
    break;
    case 7: 
    printf("Sunday");
    break;
    default: 
    printf("Invalid Choice");
  }
 return 0;
}


Output:

Enter the number between 1 to 7: 5
Friday
Enter the number between 1 to 7: 9
Invalid Choice

Write a program in c to print the name of colour by using a switch case statement

int main()
{
  int a;
  printf("Enter the number between 1 to 7 : ");
  scanf("%d",&a);
  switch(a)
  {
    case 1: 
    printf("Violet");
    break;
    case 2: 
    printf("Indigo");
    break;
    case 3: 
    printf("Blue");
    break;
    case 4: 
    printf("Green");
    break;
    case 5: 
    printf("Yellow");
    break;
    case 6: 
    printf("Orange");
    break;
    case 7: 
    printf("Red");
    break;
    default: 
    printf("Black and White");
  }
 return 0;
}

Output:

Enter the number between 1 to 7: 7
Red
Enter the number between 1 to 7: 9
Black and White

I hope you have understood how to use switch case in c programming. If you know the Hindi language, then you can watch my YouTube video on the same topic.

Thanks for reading and watching.

FAQs

Can we use logical operators in switch statement in c?

No, we can not use logical operators in switch statement in c programming.

Can we use char in switch case in C?

Yes. We can use integer or char in switch case in c.

Can we use OR operator in switch case?

As I said above, Logical operators can not be used in switch case in c. The OR is a logical operator.

What is nested switch in C?

We can use switch case statement in another switch case statement.

Is break needed in every switch case?

No, it is optional. After executing a case, the break will help you to come out of switch statement. In the absence of a break keyword, the next case will be executed. See the following example:
switch with a break statement
int main()
{
int a=1;
switch(a)
{
case 1:
printf(“This is case 1”);
break;
case 2:
printf(“This is case 2”);
}
return 0;
}
Output
This is case 1
switch without using the break statement
int main()
{
int a=1;
switch(a)
{
case 1:
printf(“This is case 1”);
case 2:
printf(“This is case 2”);
}
return 0;
}
Output
This is case 1
This is case 2

In this example, the case is not terminated here.

Can switch case be used for all data types in c programming?

We can use only char or int data-type in switch case in c programming.

Should default be the last case in a switch statement?

It is not necessary to write default in the last. You can write it anywhere in the switch case in c programming.

Can switch have same case labels?

No. All the case labels must be unique because they are constant.

Why default case is required in a menu driven program?

Menu driven programs are those programs where we give choices to user. That means we show him list options and according to his choice, he select any option. Such type of program can be written using switch case. As we know, number of choices means number of cases in switch statement.
So there is a possibility that the user can enter another option other than the given options. For such situations, we use default in menu driven program.

Does switch case work with Strings in C?

No, only integer or character work with switch case in c language.

Is switch a reserved word in C language?

Yes, switch is reserved word in c language. Reserved words or keywords are the same thing.

Leave a Comment