switch statement in C with example programs

This blog post was written and published to explain the "switch" statement in the C programming language. So, without further ado, let us begin with a brief overview.

The "switch" statement is similar to "if...else if...else," in that it is used when we need to execute a block of code based on case value. The term "switch" is sometimes used to refer to the modern or efficient version of the "if...else if...else" statement. Because instead of writing multiple if...else conditions, we can put the expression in one place and match and execute a block of code based on the value evaluated by the expression.

The general form of the "switch" statement in C is as follows:

switch(expression)
{
   case value1:
      // If "value1" matches the value
      // obtained after evaluating the "expression,"
      // run this block of code. 
      break;
      
   case value2:
      // If "value2" matches the value
      // obtained after evaluating the "expression,"
      // run this block of code. 
      break;
      
   case value3:
      // If "value3" matches the value
      // obtained after evaluating the "expression,"
      // run this block of code. 
      break;
      
   .
   .
   .
   case valueN:
      // If "valueN" matches the value
      // obtained after evaluating the "expression,"
      // run this block of code. 
      break;
   
   default:
      // Evaluate this block of code
      // if no match is found.
}

The "break" statement is used to break or stop the further execution of the "switch" statement.

The "default" statement is optional.

switch statement example in C

Consider the following program as an example of a "switch" statement in the C programming language:

#include <stdio.h>
int main()
{
   int m = 3;

   switch(m)
   {
      case 1:
         printf("January");
         break;
      case 2:
         printf("February");
         break;
      case 3:
         printf("March");
         break;
      case 4:
         printf("April");
         break;
      case 5:
         printf("May");
         break;
      case 6:
         printf("June");
         break;
      case 7:
         printf("July");
         break;
      case 8:
         printf("August");
         break;
      case 9:
         printf("September");
         break;
      case 10:
         printf("October");
         break;
      case 11:
         printf("November");
         break;
      case 12:
         printf("December");
         break;
   }

   return 0;
}

The output should be:

March

Before closing the discussion on the "switch" statement, let me include another example.

#include <stdio.h>
int main()
{
   int num1, num2, res;
   char choice;

   do
   {
      printf("1. Addition\n");
      printf("2. Subtraction\n");
      printf("3. Multiplication\n");
      printf("4. Division\n");
      printf("5. Modulus\n");
      printf("6. Exit\n");

      printf("Enter your choice (1-6): ");
      scanf("%c", &choice);

      switch (choice)
      {
      case '1':
         printf("Enter the two number: ");
         scanf("%d%d", &num1, &num2);
         res = num1 + num2;
         printf("%d + %d = %d", num1, num2, res);
         break;

      case '2':
         printf("Enter the two number: ");
         scanf("%d%d", &num1, &num2);
         res = num1 - num2;
         printf("%d - %d = %d", num1, num2, res);
         break;

      case '3':
         printf("Enter the two number: ");
         scanf("%d%d", &num1, &num2);
         res = num1 * num2;
         printf("%d * %d = %d", num1, num2, res);
         break;

      case '4':
         printf("Enter the two number: ");
         scanf("%d%d", &num1, &num2);
         res = num1 / num2;
         printf("%d / %d = %d", num1, num2, res);
         break;

      case '5':
         printf("Enter the two number: ");
         scanf("%d%d", &num1, &num2);
         res = num1 % num2;
         printf("%d %% %d = %d", num1, num2, res);
         break;

      case '6':
         return 0;

      default:
         printf("Wrong choice..!!\n");
      }
      printf("\n..........................\n");

   } while (choice != 6 && choice != getchar());

   return 0;
}

The following snapshot shows the initial output produced by the above program.

c switch case

No Type a number, say "1," and hit the ENTER key to perform the addition. After going with option 1, type any two numbers in such a way that you first enter the first number, say 10, and hit the ENTER key, then enter the second number, say 30, and hit the ENTER key to perform the addition. The following snapshot shows the sample run regarding these inputs:

c switch case example

The program continues its execution until you type "6" and hit the ENTER key to exit the program. The following snapshot shows the sample run when I typed 6 and hit the ENTER key.

c switch case program

Please note: The "do while" loop is described in its own separate post.

C Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!