Switch Case in C | Switch Case in C- Programing Language | Switch Conditions in C

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?" );
     }		
<pre>
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:
 
<pre lang="c">
     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;
 
     }
     .
     .
/**************************************************************************
 *
 * Purpose: Program to demonstrate the 'switch/case' structure.
 * Method:  Prog looks at the number of parameters passed to it and 
 *          tells the user how many its got.
 * Author:  M J Leslie
 * Date:    09-Apr-94
 *
 **************************************************************************/

Basic switch example.

main(int argc, char *argv[])
{
 
  switch (argc)			/* Switch evaluates an expression (argc)  */
  {
				/* If expression resolves to 1, jump here */
  case 1:
    puts("Only the command was entered.");
    break;		        /* break - cases the execution to jump
				   out of the 'switch' block.             */
 
				/* If expression resolves to 2, jump here */
  case 2:
    puts("Command plus one parm entered");
    break;
 
				/* If expression resolves to 3, jump here */
  case 3:
    puts("Command plus two parm entered");
    break;
 
				/* Any other value jumps here.            */
  default:
    printf("Command plus %d parms entered\n", argc-1);
    break;
  }
}

For More example Click Here

If you have questions, please ask below

4 Comments


  1. sheetal says:

    plz send me this page pz……..

  2. sheetal says:

    plz send me this page.it contain good questions.

  3. K.Ashok Kumar says:

    Please send the interviews questions about c and C++

Leave a Reply

If you have any questions headover to our forums

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>