next up previous contents index
Next: The break, continue and Up: Control Structures Previous: for   Contents   Index


switch

switch is C's equivalent of Pascal's case construction.

  
int option;
/* .. */
switch (option) {
case 1: 
  printf("option 1 selected");
  /* .. */
  break;  
case 2: 
  printf("option 2 selected");
  break;
default:
  printf("unknown option selected");
  break;  
}
Note that option must be a numerical type, and that a break statement must be used if you do not want control to `fall through' to the following cases:

  
int option;
/* .. */
switch (option) {
case 1: 
  printf("option 1 selected"); 
case 2: 
  printf("option 2 selected");
/* .. */
}
If option is one, both messages will be displayed.



CATAM admin 2010-02-23