Switch Statement

In C programming language switch statements are used to select of the alternatives from several alternatives. Switch statement is one of the decision making and branching statements. We know that when one of the alternatives is to be selected , we can use an if statement to control the selection. However, the complexity of such a program increses dramatically when the number of alternatives increases. The program becomes difficult to read and follow. At times, it may confuse even the person who have designed it. Fotunately, the C programming language has a built in multiway decision staement known as a switch. The switch statement tests the value of given variable ( or expression) against a list of case values and when a match is found, a block of statments associated with that case is executed. The general form of switch statement is as shown below –

switch (expression)
{
   case value-1:
                  block -1
                  break;

   case value-2:
                  block - 2
                  break;

   -------------
   -------------

   default:
                  default - block
                  break;
}
statement-x;

The expression is an integer expression or characters. Value-1, value-2 ….. are constants or constant expressions and are known as case labels. Each of these values should be unique within a switch statement. Block-1, block-2 …… are statement lists and may contain zero or more statements. There is no need to put braces around these blocks. Note that case labels end with a colon( : ).

When the switch is executed, the value of the expression is successfully compared agaisnt the values value-1, value-2 ,…… if a case is found whose value matches with the value of the expression, then the block of statements that follows the case are executed.

The break statement at the end of each block signals the end of the particular case and causes an exit from the switch statemetn, transferring the control to the statements following the switch.

Below is the sample C program of switch statement in the C programming language of  to select a random color from some choices-

#include<stdio.h>
#include<string.h>
void main()
{
     int n;
     
     printf(" Enter 1 to 4 to select random color");
     scanf("%d",&n);
     
     switch(n)
     {
              case 1:
                   printf("You selected Red color");
                   break;
              case 2:
                   printf("You selected green color");
                   break;
              case 3:
                   printf("You selected yellow color");
                   break;
              case 4:
                   printf("You selected blue color");
                   break;
              default:
                      printf("No color selected");
     }
     getch();
}

Rules for switch statements –

  • The switch expression must be an integral type
  • Case labels must be constants or constant expressions
  • Case labels must be unique. No two labels can have the same value
  • Case labels must end with semicolon.
  • The break statement transfers the control out of the switch statement
  • The break statement in optional. That is, two or more case labels may belong to the same statements.
  • The default label is optional. If present, it will be executed when the expression does not find a matching case label.
  • There can be at most one default label.
  • The default label may be placed anywhere but usually placed at the end
  • it is permitted to nest switch statements.

yashan has written 70 articles

One thought on “Switch Statement

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>