Switch-case-default statement

INTERODUCTION:-

    To select from multiple branching , apart from using nested if-else statements, we can use another special type of construct .  The particular construct successively tests the value of an expression or variable against a list of integers or character constants. When a match is found the statement associated with that constant that constant are executed.

FLOWCHART:-

syntax:- 
switch(integer_or_charecter_variable)
 {    case  constant 1:   statements;
      case  constant 2:   statements;
      case  constant n:   statements;
       default:   statements;
 } 

The following features of the switch-case – default structure should be noted:

  1. One can use only integer or character values for the  case constants. Floating point variables are not allowed as a case constant.
  2. One can have only equality of cases and we cannot have any other inequality operation ( like > , <etc.)
  3. Each case constant is followed by a colon (:)
  4. There can be multiple statements following a case constant and these statements need nod not be enclosed within a pair of curly brackets { }
  5. The default statements is optional and can be placed anywhere within the switch block.
  6. After entering  the switch block each case constant is matched with the switch variable. If a match is not found with a case constant , then the control skip next case constant.
  7. As soon as the switch value matches with a case constant, the statements following that case , and all statements associated with subsequent cases along with the default statement get exectuted.
  8. To execute only the statements following a particular case , a break statement is included after the statements following the case constant . Once a case is satisfied the controls simply falls through the subsequent statements till it reaches a break statement.
  9. A switch – case default structure is generally used to write  amenu driven program, where each option of the menu  corresponds to a case constant.

3 comments

Leave a comment