Switch statement in c

A switch-statement is a conditional statement through which we pass a variable constant. A variable constant is a value provided by the user which is passed through the switch statement inside the switch block. This value is compared with all the cases inside the switch block until the value is matched with any of the switch cases. 

When the match is found, the statement assigned to the particular case is executed.

If the match is not found, then the default case gets executed, and the control goes outside the switch block.


Below is the syntax for the switch statement.


switch(expression or variable)
 {
   case constant 1: //statements;
                   break; //optional
   case constant 2: //statements;
                   break; //optional
   case constant 3: //statements;
                   break; //optional
   .
   .
   .
   case constant n://statements;
                   break;//optinal
   default: exit(0);
 }

      The expression or constant is the variable value provided by the user. The variable can be an integer variable or character variable.

       constant 1, constant 2, constant 3....constant n are the cases inside the switch block.

      We can understand the switch statement better with the help of an example.

    #include<stdio.h>
    int main()
    {
     char grade;
    // clrscr();
     printf("\n Enter the grade\n");
     scanf("%c",&grade);
       if(grade>=97 && grade<=122)           //if the input grade is in lower case, it is  converted into upper case
       grade=grade-32;
     switch(grade) 
    {                                         // this switch keyword consist of a body, and grade is passed to the body through switch keyword.
      case 'O': printf("\n Excellent");                  
              break;
      case 'A': printf("\n Very good");
              break;
      case 'B': printf("\n Good");
              break;
      case 'C': printf("\n Average");
              break;
      default: exit(0);
     }
     return 0;
    }
    Output:
     
    switch-statement-c

    Explanation: 
         In the above program, the character variable "grade" is taken as input and passed inside the switch block. The variable value is compared with the cases inside the switch block. If the grade is O, the statement inside the case 'O' will be executed.
          If the grade is A the control will move to case ‘A’ and the statement inside case ‘A’ will be executed. similar will happen to other cases.
         If the variable passed inside the switch statement is other than the cases O, A, B, C then the default statement is executed and the control will come out of the program.
     Use of the break keywordas you can see in the program, after every case ‘break’ keyword is given, 
     What is  the use of the break keyword?
         Depending upon the variable passed, when the control jumps to a particular case, the statement inside the case gets executed because of the break keyword the control comes out of the switch block, and the remaining cases are not executed, but when the break keyword is not given, the compiler executes the particular case as well as it executes the remaining cases. 
     To know more about the break statement, follow the below link,


Rules to remember for writing a switch case program::

1. When the variable passed through switch keyword is a character, then it must always be written in single quotes ‘ ‘. For example, case ‘A’ or case ‘a’.
2. When the variable passed through switch keyword is a number, then there is no need for writing it in single quotes. For example, Case 1.
3. If you write the integer case in single quotes. For example., case ‘1’ then it will take the ASCII value of 1.
4.There should be always a space between a keyword case and variable. Eg. case ‘A’.

Example2:
Write a c program to print a day between Monday to Sunday using a switch statement. 


 
#include<stdio.h>
#include<stdlib.h>

int main() 
 { 
  int day;
  printf("Enter the day:");
  scanf("%d",&day);
  switch(day)
  {
   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: exit(0);
     } 
    return 0;
}

Output:
Enter the day:
4
Thursday

Explanation:
Integer variable "day" is taken as input, and the input value is passed inside the switch block.
As written in the code, there are seven cases from 1 to 7, And the default is the last.
If the input is 1, then the statement inside case 1 will be executed.
We have a break statement after every printf statement. Hence, after the printf statement, the control will come out of the switch statement, and the program will end.
If the input is 4, Thursday will be printed, and so on.
When the input is other than 1 to 7 then, the control will go to the default case, and the program will exit.
#include<stdlib.h> header file is used because it contains exit(0); function in its library file.
Some c compilers may support the exit(0); function without these files, some may not support.
Also, see: