Switch Case Control Structure in C

In real life we are often faced with situations where we are required to make a choice between a number of alternatives rather than only one or two.

For example, what subject to choose after 10th, which school or college to join or which place to visit. We almost always end up making a wrong decision is a different matter altogether!.

Serious C programming is same; the choice we are asked to make is more complicated than merely selecting
between two alternatives. C provides a special control statement that allows us to handle such cases effectively; rather than using a series of if statements. This control instruction is called the Switch case control structure.

Decisions Using switch

The control statement that allows us to make a decision from the number of choices is called a switch, or more correctly a switch case-default, since these three keywords go together to make up the control statement.

Switch case Statement

switch ( integer expression )

{

case constant 1 :
do this ;
case constant 2 :
do this ;
case constant 3 :
do this ;
default :
do this ;

}

Description of above Switch case flow:

The integer expression would be an integer value. It could be an integer constant like 1, 2 or 3, or an expression that evaluates to an integer.However char can also be used in switch case.

The keyword case is followed by an integer or a character constant. Each constant in each case must be different from all the others.

The “do this” lines in the above form of switch represent any valid C statement.

What happens when Switch program execute?

  • The integer expression is evaluated.
  • The value it gives is then matched, one by one, against the constant values that follow the case statements.
  • When a match is found, the program executes the statements following that case, and all subsequent case and default statements as well.
  • If no match is found with any of the case statements, only the statements following the default are executed. A few examples will show how this control structure works.

Let’s have an example:

main( )
{
int i = 2 ;
switch ( i )
{
case 1 :
printf ( “case 1 \n” ) ;
case 2 :
printf ( “case 2 \n” ) ;
case 3 :
printf ( “case 3 \n” ) ;
default :
printf ( “default case \n” ) ;
}
}

The output of this program would be:

case 2
case 3
default case

The output is what we have not expected!

  1. We didn’t expect the second and third line in the above output.
  2. The program prints case 2 and 3 and the default case.
  3. We said the switch executes the case where a match is found and all the subsequent cases and the default as well.

But What next : Use of break in Switch case

If you want that only case 2 should get executed, it is up to you to get out of the switch then and there by using a break statement. The following example shows how this is done. Note that there is no need for a break statement after the default, since the control comes out of the switch anyway.

Let’s have an example with the use of break

main( )
{
int i = 2 ;
switch ( i )
{
case 1 :
printf ( “case 1 \n” ) ;
break ;
case 2 :
printf ( “case 2 \n” ) ;
break ;
case 3 :
printf ( “case 3 \n” ) ;
break ;
default :
printf ( “default case\n” ) ;
}
}

Now output would be as follow:

case 2

Switch case flowchart

switch case flow chart
switch case flow chart

 

 

[xyz-ihs snippet=”Discuss”]

Leave a Reply