November 2001
Beginner
1128 pages
29h 12m
English
Suppose you create a screen menu that asks the user to select one of five choices, for example, Cheap, Moderate, Expensive, Extravagant, and Excessive. You can extend an if else if else sequence to handle five alternatives, but the C++ switch statement more easily handles selecting a choice from an extended list. Here's the general form for a switch statement:
switch (integer-expression) { case label1 : statement(s) case label2 : statement(s) ... default : statement(s) }
A C++ switch statement acts as a routing device that tells the computer which line of code to execute next. On reaching a switch, the program jumps to the line labeled with the value corresponding to the value of integer-expression. For example, if integer-expression ...