Switch case in C


Hi friends here comes one of the important tutorials in C, switch case. Switch case statement is a multi way decision statement; it is as similar as a multiple decision statements by using if-else. Switch statement evaluates the conditional expression and tests it again

numerous constant values. A corresponding branch (case) where the expression matches is taken during execution.

The switch case statements allow controlling complex conditional and branching operations. It includes any number of case instances, but should not have the same value. The value of the expressions in a switch-case statement must be an ordinal type i.e. integer, char, short, long, etc.

Break statement causes an exit from switch statement, so it does not keep going through each case. We should be having one case called default; this is executed if none of the other cases matches conditional expression.

Typical syntax:

 

switch ( expression ) {

               case  program statement:

                       program statement;

                       program statement;

                       ......

                       break;

               case  value_2:

                       program statement;

                       .......

                       break;

               case  value_n:

                       program statement;

                       .......

                       break;

               default:

                       .......

                       .......

                       break;

        }

 


In above syntax, switch, case, break are keywords.

Rules of Using Switch Case in C Programming

      • Values for ‘case must’ be integer or character constants.
      • Floating point values are not allowed as case label.
      • Switch case should have one default label.
      • Const Variable is allowed in switch Case Statement.
      • The order of the ‘case’ statements is unimportant.
      • Case Label must be unique
      • Case labels must have constants / constant expression
      • Case labels must end with (:) colon.
      • Empty Switch case is allowed.
      • Each compound statement of a switch case should contain break statement to exit from case.
      • Macro Identifier are allowed as Switch Case Label.
      • Two or more cases may share one break statement
      • The default clause may occur first (convention places it last)
      • Comparion operators are not accepted
      • Default label is Optional
      • Nesting ( switch within switch ) is allowed.

Advantages of Using Switch statement:

      • easier to debug
      • faster execution potential
      • easier to read
      • easier to understand
      • easier to maintain

Example:

#include <stdio.h>   
Void main() 
{
char grade;
printf(“Please enter the grade value: \n”);

scanf(“%c”,&grade);

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;

        }

getch();

}

 

Related posts:

About author

This article was written by admin

Admin has over twenty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles. Working presently as Development Manager in India. A firm Believer in Knowledge grows when it shared.

Comments

No Comments

Leave your comment

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