Switch case

Case control ( Switch-case-default ) Structure :-
                                                                                        After studing about if-else statement we know that  if we want to execute set of instructions for some special condition  and some other set of instruction for some other condition , we use if-else statement . BUT if we have several different situations(cases) and also their corresponding statements , so in this situation it is difficult to use if-else structures because It make the program complex and is difficult to handle and the chances of mistake increase . It is also difficult to detect logical Error in this type of problem and also difficult to maintain the code . for this problem C provide us a structure called Case control structure or more correctly say switch-case-default structure . In this structure it is very easy to select one out of several alternatives . It not only provide more Flexibility to the program but also it is easy to handle from if-else statements .
Syntax :-
          switch( expression )
          {
                  case constant1 :
                       statement-block1 ;
                       break ;
                  case constant2 :
                       statement-block2  ;
                       break ;
                  case constant3 :
                       statement-block-3  ;
                       break ;
                  default :
                       statement-block-default ;
          }
In this 
switch  , case , default   -----> are keywords
expression                     -----> It must give integer or character value
                                                      < Float value is not allowed >
constant-n                      -----> It is the identification given to the choice .
                                                 It can be integer or character constant But float is not allowed.
                                                 We can use arithmetic expressions like
                                                  case 4 + 5 :  correct
                                                 But  use of variable is not allowed .ie. case a+b : Wrong 
break                              -----> is a keyword . It is use to end the switch structure and stop
                                                further execution of other "case" . It shift the control outside 
                                                the switch structure .
Now we try to see how it actually work .
The resulting value of expression is matched with every case constant .
If the value match any of the case constant so it start executing from its statement-block  and it goes executing statement-block Till it encounter break statement , due to this break statement the control is transferred outside the switch structure .
If No match is found , it start executing the default-statement-block .

The flow-chart shows clear diagrametical representation ,that How switch-case structure works.

switch case

One more thing is that we can place case-constants in any order ( ie. incrementing or decrementing ) or without order . Also the default can  be place on any position . ie.

          switch( expression )
          {
            case 8 :
               statement-block-8 ;
               break ;
            case 5 :
               statement-block-5 ;
               break ;
            default :
               statementblock-default ;
               break ;
            case 11 :
               statement-block-11 ;
               break ;
            case 4 :
               statement-block-4 ;
               break ;
          }

In character constant can be used in switch , In this the ASCII value of character is compair with each case value .

I want to tell you some points which must be remember during the use of switch-case-default . These points help you to increase your programming skill .
We can use any valid expression of character or integer type in switch . But Float type is Not allowed .
If we use character type in switch so the value of character expression , which is matched with case-value is ASCII value of character ie. case 65 is equal to case 'A' .            ( ASCII value of character 'A' is  65 )
It is not allowed to use variables in case-value (ie.  case a + b :  is not allowed ) .
We must need to use break statement after the end of each statement-block . we use break statement to stop further execution of other case's .
We can only use constant after case keyword ,variable are not allowed .
switch structure can also be used in Nerted form , But it is rarely used .
switch structure is mainly used for Menu based programs .
The value of two case-constants can not be equal . We can use both character and integer constant values in same switch but we should concentrate on the value choosen because it only match integer value . ie .
        switch( expression )
        {
          case 65 :
              statement-block65 ;
              break ;
         case 'A' :
              statement-block-A ;
         }

ERROR 
The ASCII value of character 'A' is equal to 65

Who's online

We have 17 guests and no members online

About Us

Harsimran Singh

Hello

This website is a special gift for Programming Lovers , which want to learn basic Programming Languages and want to develop there skills Read More....

Our work

Find Us

This email address is being protected from spambots. You need JavaScript enabled to view it.

Harsimran Singh(Facebook)

Harsimran Singh(about.me)

Harsimran Singh(G+)