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, which school to join or which hostel to visit or still harder which girl to marry ( you 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 in fact the topic of this chapter . towards the end of the chapter we would also study a keyword called goto, and understand why we should avoid its usage in C programming.
The out put of the program would be:
I am in case 2
I am in case 3
I am in default
The output is definitely not what we expected we didn't expect the second and third line in the above output. The program prints case 2 and 3 and the default case. well, yes. we said the switch executes the case where a match is found and all the subsequent cases and the default as well.
If you want that only case 2 should get executed, it is upon you to get out of the switch then and there by using a break statement. The following examples 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 any way.
main()
{
int i=2;
switch (i)
{
case 1:
printf( " i am in case 1");
break;
case 2:
printf ( "i am in case 2");
break;
case 3:
printf ( "i am in case 3" );
break;
default:
printf ( "i am in default");
}
}
the output of the program would be:
I am in case 2
Decision 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. The most often appear as follows:
switch ( integer expression )
{
case constant 1:
do this ;
case constant 2:
do this ;
case constant 3:
do this ;
do this ;
default:
do this ;
}
What happens when we run a program containing a switch? first , the integer expression following the keyword switch 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 statement 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.
Consider the following program:
main ()
{
int i = 2
switch (i)
{
case 1:
printf (" i am in case 1 \n");
case 2:
printf ( "i am in case 2 \n");
case 3 :
printf ( "i am in case 3 \n");
default:
printf ( "i am in default \n");
}
}
I am in case 2
I am in case 3
I am in default
The output is definitely not what we expected we didn't expect the second and third line in the above output. The program prints case 2 and 3 and the default case. well, yes. we said the switch executes the case where a match is found and all the subsequent cases and the default as well.
If you want that only case 2 should get executed, it is upon you to get out of the switch then and there by using a break statement. The following examples 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 any way.
main()
{
int i=2;
switch (i)
{
case 1:
printf( " i am in case 1");
break;
case 2:
printf ( "i am in case 2");
break;
case 3:
printf ( "i am in case 3" );
break;
default:
printf ( "i am in default");
}
}
the output of the program would be:
I am in case 2