Switch in C Programming

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

INTRODUCTION TO C PROGRAMMING

Programming with C an introduction part two

Data types for C programming


C PROGRAMMING CHARACTER SET

CONSTANTS IN C PROGRAMMING

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

ARITHMETIC INSTRUCTIONS TO WRITE C PROGRAM

CONVERSION OF CONSTANTS IN C PROGRAM

PRIORITY OF AR THEMATIC OPERATIONS IN C

OPERATORS ASSOCIATIVITY IN C

IF STATEMENT

MULTIPLE STATEMENTS IN IF

IF AND ELSE

NESTED IF AND ELSE


BREAK

CONTINUE AND DO WHILE IN C LANGUAGE

SWITCH IN C PROGRAMMING

FUNCTIONS IN C PROGRAMMING


Functions and usage in C part two

Coding in C functions

Software testing spiral model

No comments:

Post a Comment