Switch – Case
(Menu-Driven program)
            Definition: The switch keyword is a selection statement that allows to transfer the 
            program control on the matching case depending upon the value of the variable or 
            expression. 
     
        ·             The body of switch terminates as soon as the break statement appears.
        ·             If the matching case is not found then the switch statement transfers itself  
                to default case and executes the statements there in the default case.
  
      Keywords :

i.        switch – It is used to transfer the program control to the 
matching case depending upon the value of variable or expression.

ii.        case – It contains one or more executable statements that end 
with a break statement.


iii.        break – It is a jump statement that terminates the execution of 
the current case and comes out of the body of the switch.

iv.        default – It is the last statement of the switch body and executes
 if the matching case is not found. It is an optional statement and can 
be used whenever requires.



   SYNTAX:      
      switch (value or expression)
   {
     case constant-1:
                        Statement-block-1
              break;
      case constant-2:
                        Statement-block-2
              break;
      case constant-3:
                        Statement-block-3
              break;
      default:
                        Statement-block-default
     }
     

 Fall  through
  Definition:  The absence of break statement from one or more case(s) within the
   switch statement generates fall through situation. In this case the control falls through
   all the cases until it findsbreak statement. As soon as a break statement appears,
   the control stops falling.

Note: default and break statements are optional.


2 Comments

Post a Comment

Previous Post Next Post