Switch Case in C Programming

“switch case ” is a case control structure.

 It contains case and default values.

“switch” statement takes a value as a expression for equality testing against a list of values/case.

Switch case statement is used in programming to select one amont multiple options.

syntax for a switch statement:-

switch (expression) {
case constant-expression:
                            statement(s);
                            break; /* optional */

case constant-expression:
                             statement(s);
                             break; /* optional */

                       /* you can have any number of case statements */
default:
 /* Optional */
                   statement(s);
}

Rules of switch statement −

In a “switch” statement we can have any number of case.

for equality testing against a list of values/case, constant-expression for a case must be the same data type as the variable in the switch expression.

When “switch” expression value is equal to a case, the statements following that case will execute until a break statement is reached/executes.

When a break statement is reached/executes, the switch statement terminates, and the control jumps to the outside of switch.

break is a optional. If there is no break in the switch case, the flow of control will jump to the next case of switch until a break is reached.

switch statement also have a default case, which appear at the end of the switch. when none of the cases is true  then default case will execute.

No break is needed in the default case. “default” case is also an optional.

Example: Write a program to take one numbers from user between 1 to 3. Write entered number in words if number between 1 to 3 otherwise print wrong number using switch.

#include 
int main() {
   int n;
   printf(" enter number between 1 to 3");
   scanf("%d", &n);
   switch (n) {
   case 1:
      printf("\n one");
      break;
   case 2:
      printf("\n two");
      break;
   case 3:
      printf("\n three");
      break;
   default:
      printf("\n wrong number");
   }
   return 0;
}

OUTPUT

enter number between 1 to 3
 2	// user entered 2	
 two	

Note: In the above program, for example user enters a number 6 then output will be as follows:

enter number between 1 to 3
 6	// user entered 6	
 wrong number

Example: Write a program using switch statement but without break.  

#include 
int main() {
   char c = 'B';

   switch (c) {
   case 'A':
      printf("Excellent!\n");
      break;
   case 'B':
   case 'C':
      printf("Well done\n");
   case 'D':
      printf("You passed\n");
      break;
   case 'F':
      printf("Better try again\n");
      break;
   default:
      printf("Invalid Character  \n");
   }
   return 0;
}

OUTPUT

Well done
You passed

Description: Because if there is no break in the switch case, the flow of control will jump to the next case of switch until a break is reached.

Note: In the above program, for example value of char variable c = ‘ A’ then the output will be as follows:

Excellent

Nested switch case

In C programming language we can define switch statement within another switch.

Syntax for a nested switch statement: −

switch (ch1) {
case constant - expression:
   printf("case A of outer switch");

   switch (ch2) {
   case constant - expression:
      printf("case  A of inner switch");
      break;
   case constant - expression:
      /* case code */
   }

   break;

case 'B':
   /* case code */
}

Read More

  1. C++ program for student details
  2. Anonymous Object in C++
  3. Lowercase character to uppercase character
  4. Command Line Argument
  5. Local vs Global Object in C++
  6. Local and Nested Classes in C++
  7. Arithmetic operations using switch case
  8. Constructor in C++
  9. Know more about C++