switch

Syntax:

    switch( expression ) {
    case A:
    statement list;
    break;
    case B:
    statement list;
    break;
    ...
    case N:
    statement list;
    break;
    default:
    statement list;
    break;
    }

The switch statement allows you to test an expression for many values, and is commonly used as a replacement for multiple if()…else if()…else if()… statements. break statements are required between each case statement, otherwise execution will “fall-through” to the next case statement. The default case is optional. If provided, it will match any case not explicitly covered by the preceding cases in the switch statement. For example:

     char keystroke = getch();
     switch( keystroke ) {
       case 'a':
       case 'b':
       case 'c':
       case 'd':
         KeyABCDPressed();
         break;
       case 'e':
         KeyEPressed();
         break;
       default:
         UnknownKeyPressed();
         break;
     }

Related Topics: break, case, default, if