Next: Functions
Up: Control Structures
Previous: while
char ch;
cin >> ch;
switch (ch) {
case 'l':
cout << "List of Records";
printListOfRecords();
break;
case 'h':
case '?':
printHelpScreen();
break;
default:
cout << "Invalid input!";
}
- Switch variables must be numeric or char, not strings.
- As you can have more than one statement in each case, you need a
break after each case, otherwise the program execution
``falls through'' to the next case (in the example, if ``h'' is
pressed, the same code gets executed as for ``?'').
- The default gets executed whenever none of the
cases applies.
Andreas Koeller
2000-06-04