{ A destination to learn coding }

Conditional Statements

Switch statement

Switch is a multiple-branch selection statement.

General Form
switch(expression)
{
  case constant1:
    statement;
    break;

  case constant2:
    statement;
    break;

  .
  .
  .
  .
  default :
   statement;
}

The expression used in switch() can only be of an integer or a character type.

Let us see the example below
#include<stdio.h>
int main()
{
  int a;
  printf("Enter any number from 1 to 4\n");
  scanf("%d",&a);

  switch(a)
  {
    case 1:
     printf("One\n");
     break;

    case 2:
      printf("Two\n");
      break;

    case 3:
      printf("Three\n");
      break;

    case 4:
      printf("Four\n");
      break;

    default:
     printf("Invalid Input\n");
  }

  printf("successful");

  return 0;
}

Let us analyse the code above.
If the value of a is 1, the first case will be executed.
If the value of a is 2, the second case will be executed and so on.
If the user enters some value other than 1,2,3 and 4, the default statement will be executed.
After the execution of the switch block, the statement right after the switch block is executed.

break;

break is used to terminate the sequence.
If break is omitted, the subsequent cases will be executed resulting in unnecessary duplication of statements.
Use of break tag makes the code more efficient.

If the input is of character type, you need to enclose the character in ' '

See the example below.
#include<stdio.h>
int main()
{
  char a;
  printf("Enter any one of the letters, (a,b,c,d)\n");
  scanf("%c",&a);

  switch(a)
  {
    case 'a':
      printf("The character is a\n");
      break;

    case 'b':
      printf("The character is b\n");
      break;

    case 'c':
      printf("The character is c\n");
      break;

    case 'd':
      printf("The character is d\n");
      break;

    default:
      printf("Invalid Input\n");
  }

  return 0;
}
Try this
How to make a calculator using switch
C Codes