The switch statement is C/C++’s multiway branch statement. It is used
to route execution one of several different ways. The general form of the statement is

 switch (expression) {      case constant 1: statement sequence 1;           break;      case constant 2: statement sequence 2;           break;   .   .   .      case constant N: statement sequence N;           break;      default: default statements; 

Each statement sequence may be from one to several statements long. The default portion is optional. Both expression and the case constants must be integral types.

The switch works by checking the expression against the constants. If a match is found, that sequence of statements is executed. If the statement sequence associated with the matching case does not contain a break, execution will continue on into the next case. Put differently, from the point of the match, execution will continue until either a break statement is found or the switch ends. If no match is found and a default case is existent, its statement sequence is executed. Otherwise, no action takes place. The following example processes a menu selection:

switch(ch) {   case 'e': enter();     break;   case 'l': list();     break;   case 's': sort();     break;   case 'q': exit(0);     break;   default:      cout << "Unknown command!\n";     cout << "Try Again.\n"; }

C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net