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 syntax for this is


switch ( integer expression )
{
case constant 1 :
do this ;
case constant 2 :
do this ;
case constant 3 :
do this ;
default : do this ;
}

The integer expression following the keyword switch is any C expression that will yield an integer value. It could be an integer constant like 1, 2 or 3, or an expression that evaluates to an integer. 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.

How it functions ?

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 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.

Example with only Switch

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" ) ;
}
}

The output of this program would be:

I am in case 2
I am in case 3
I am in default

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 upto you to get out of the switch then and there by using a break statement.There is no need for a break statement after the default, since the control comes out of the switch anyway.

Example with Break statement and Switch

main( )
{
int i = 2 ;
switch ( i )
{
case 1 : printf ( "I am in case 1 \n" ) ;
break ;
case 2 : printf ( "I am in case 2 \n" ) ;
break ;
case 3 : printf ( "I am in case 3 \n" ) ;
break ;
default : printf ( "I am in default \n" ) ;
}
}

The output of this program would be:

I am in case 2

Other C programming Related topics are


PROGRAMMING C VARIABLES
C PROGRAM INSTRUCTIONS
COMPILATION AND EXECUTION OF C PROGRAM
C PROGRAMMING RULES PART ONE
C PROGRAMMING RULES PART TWO
COMPILATION AND EXECUTION OF C PROGRAM
INSTRUCTIONS TO WRITE C PROGRAM

Other Programming Courses :

ASP.NET part one and two
Programming with C and C Sharp
Dot Net Complete Course Part one and two
Interview Questions in dot net and asp.net part one part two
Software Testing Complete course part one and two
Interview Questions in software Testing
Complete software testing course here as basic and advanced parts .

Team blog recent posts

Work Flow in SAP ABAP an introduction
ABAP Syntax for Move
Software testing spiral model
Heat and Convection (physics)

Thank you for visiting Codes and Test. To get the updates as and when published, please subscribe to my blog via email or RSS FEED.You can mail me at d_vsuresh[at the rate of ]yahoo[dot]co[in] for specific feed backs and suggestions.

0 comments

Post a Comment