In addition to nested if-else statements, we can also use switch statement for multiple selection according to the value of an ordinal type expression.
C++ control structure | Equivalent Turbo Pascal control structure |
switch (<ordinal_expression>) { case <value_1>: <statement_1_for_case_1>; <statement_2_for_case_1>; ... <statement_n_for_case_1>; break; case <value_2>: <statement_1_for_case_2>; <statement_2_for_case_2>; ... <statement_n_for_case_2>; break; ... default: <statement_1_for_default_case>; <statement_2_for_default_case>; ... <statement_n_for_default_case>; } |
case <ordinal_expression>
of <value_1>: begin <statement_1_for_case_1>; <statement_2_for_case_1>; ... <statement_n_for_case_1> end; <value_2>: begin <statement_1_for_case_2>; <statement_2_for_case_2>; ... <statement_n_for_case_2> end; ... else (* supported in Turbo Pascal only *) <statement_1_for_default_case>; <statement_2_for_default_case>; ... <statement_n_for_default_case> end |
In this control structure, case <value>: (in C++) and <value>: (in Pascal) are case labels. The <ordinal_expression> is evaluated. The statements for the particular returned value are executed while the other are ignored. If the returned value is not in the cases listed, the statements for the default case (default in C++ or else in Turbo Pascal) will be executed. See the following program:
C++ version | Pascal version |
#include <iostream.h> main() { char grade; cout << "Grade: "; cin >> grade; switch (grade) { case 'A': case 'B': cout << "Good!" << endl; break; case 'C': cout << "Average." << endl; break; case 'D': cout << "Fair." << endl; break; case 'F': cout << "Poor." << endl; break; default: cout << "Invalid grade!" << endl; } return 0; } |
program prog_14(input, output); var grade: char; begin write('Grade: '); readln(grade); case grade of 'A', 'B': writeln('Good!'); 'C': writeln('Average.'); 'D': writeln('Fair.'); 'F': writeln('Poor.'); else writeln('Invalid grade!') end end. |
Sample output 1 (text in red is entered by user): | |
Grade: C Average. |
|
Sample output 2 (text in red is entered by user): | |
Grade: X Invalid grade! |
In C++, if the value returned by the <ordinal_expression> matches the <value> in any of the case labels, the statements starting from the case label to the break statement are executed. Therefore you may notice that there is a break statement just before the next case label. Unexpected result may be obtained if any of the break statements is missing. See the following program for an example. The following program is similar to the above program except that one break statement is missing.
#include <iostream.h> main() { char grade; cout << "Grade: "; cin >> grade; switch (grade) { case 'A': case 'B': cout << "Good!" << endl; break; case 'C': cout << "Average." << endl; // One break statement is missing here. case 'D': cout << "Fair." << endl; break; case 'F': cout << "Poor." << endl; break; default: cout << "Invalid grade!" << endl; } return 0; } |
Sample output (text in red is entered by user): |
Grade: C Average. Fair. |
As you can see from the above sample output, both "Average." and "Fair." appear on the screen when grade is 'C'. Since there is no break statement after "Average." is displayed, the next statement (i.e. displaying "Fair.") is also executed.