Switch case statements in C language

When there are several possibilities and we want to take different actions depending on each selection, we utilize the switch case statement. It is a statement with multiple possible outcomes that assesses an expression and compares its value to a set of situations. If a match is discovered, the associated code block is run. 

Basic syntax of switch-case statement:

switch(expression) {
   case constant-expression1:
      // code to be executed if expression matches constant-expression1
      break;
   case constant-expression2:
      // code to be executed if expression matches constant-expression2
      break;
   .
   .
   .
   default:
      // code to be executed if expression doesn't match any of the above cases
}

Here’s an example of the switch-case statement:

#include <stdio.h>

int main() {
   int num;
   printf("Enter a number between 1 and 5: ");
   scanf("%d", &num);
   
   switch(num) {
      case 1:
         printf("You entered 1.\n");
         break;
      case 2:
         printf("You entered 2.\n");
         break;
      case 3:
         printf("You entered 3.\n");
         break;
      case 4:
         printf("You entered 4.\n");
         break;
      case 5:
         printf("You entered 5.\n");
         break;
      default:
         printf("Invalid input.\n");
   }
   
   return 0;
}

Output:

Enter a number between 1 and 5: 1
You entered 1.

Enter a number between 1 and 5: 3
You entered 3.

Enter a number between 1 and 5: 7
Invalid input.

The user is asked to provide a number between 1 and 5 in this example. The switch case statement then evaluates the input. A message is printed to the console if the user provides a number between 1 and 5. The default message is printed if the user inputs a number that is outside of this range.

Another example of basic switch-case statement

#include <stdio.h>

int main() {
   char grade;

   printf("Enter your grade (A/B/C/D/F): ");
   scanf("%c", &grade);

   switch(grade) {
      case 'A':
         printf("Excellent!\n");
         break;
      case 'B':
         printf("Good job!\n");
         break;
      case 'C':
         printf("Not bad!\n");
         break;
      case 'D':
         printf("Could be better.\n");
         break;
      case 'F':
         printf("You failed.\n");
         break;
      default:
         printf("Invalid input.\n");
   }

   return 0;
}

Output:

If you input 'A' as the grade, the output will be "Excellent!".

If you input 'B' as the grade, the output will be "Good job!".

If you input 'C' as the grade, the output will be "Not bad!".

If you input 'D' as the grade, the output will be "Could be better.".

If you input 'F' as the grade, the output will be "You failed.".

If you input any other character, the output will be "Invalid input."

In this example, the user enters a grade (A/B/C/D/F), and the program uses a switch case to print a message based on the grade entered. If the grade is not one of the valid options, the program prints “Invalid input.”