switch/case keywords

# The 'switch' and 'case' keywords

The switch-case statement is a multi-way decision statement. Unlike the multiple decision statement that can be created using if-else, the switch statement evaluates the conditional expression and tests it against numerous constant values. The branch corresponding to the value that the expression matches is taken during execution.

The value of the expressions in a switch-case statement must be an ordinal type i.e. integer, char, short, long, etc. Float and double are not allowed.

The syntax is :

         switch( expression )
         {
            case constant-expression1:	statements1;
            [case constant-expression2:	statements2;]
            [case constant-expression3:	statements3;]
            [default : statements4;]
         }

The case statements and the default statement can occur in any order in the switch statement. The default clause is an optional clause that is matched if none of the constants in the case statements can be matched.

Consider the example shown below:

         switch( Grade )
         {
            case 'A' : printf( "Excellent" );
            case 'B' : printf( "Good" );
            case 'C' : printf( "OK" );
            case 'D' : printf( "Mmmmm...." );
            case 'F' : printf( "You must do better than this" );
            default  : printf( "What is your grade anyway?" );
         }

Here, if the Grade is 'A' then the output will be

         Excellent
         Good
         OK
         Mmmmm....
         You must do better than this
         What is your grade anyway?

This is because, in the 'C' switch statement, execution continues on into the next case clause if it is not explicitly specified that the execution should exit the switch statement. The correct statement would be:

         switch( Grade )
         {
            case 'A' : printf( "Excellent" );
                       break;

            case 'B' : printf( "Good" );
    	           break;

    	case 'C' : printf( "OK" );
    	           break;

            case 'D' : printf( "Mmmmm...." );
    	           break;

    	case 'F' : printf( "You must do better than this" );
    	           break;

    	default  : printf( "What is your grade anyway?" );
                       break;
    	}

Although the break in the default clause (or in general, after the last clause) is not necessary, it is good programming practice to put it in anyway.

An example where it is better to allow the execution to continue into the next case statement:

         char Ch;
         .
         .
         switch( Ch )
         {
           			/* Handle lower-case characters */
            case 'a' :
            case 'b' :
           	      .
    	      .
    	      .
            case 'z' :
              printf( "%c is a lower-case character.\n", Ch );
    	  printf( "Its upper-case is %c.\n" toupper(Ch) );
    	  break;

           			/* Handle upper-case characters */
    	case 'A' :
    	case 'B' :
    	      .
    	      .
    	      .
    	case 'Z' :
               printf( "%c is a upper-case character.\n", Ch );
    	   printf( "Its lower-case is %c.\n" tolower(Ch) );
    	   break;

           			/* Handle digits and special characters */

            default  :
               printf( "%c is not in the alphabet.\n", Ch );
    	   break;

         }
         .
         .

## Example:

Basic switch example.

## See also:

- if keyword.

Top | Master Index | Keywords | Functions