
Available under Creative Commons-ShareAlike 4.0 International License.
Multiway selection is often needed to cover all possibilities. Assume that the user has been prompted for the ages of two people with the answers stored in variables named age1 and age2. Consider:
Example 12.2: C++ source code
if(age1 > age2) { cout << "nnnnThe first person is older."; }else { cout << "nnnnThe second person is older."; }
What if the two persons are the same age? The program incorrectly says the second person is older. To solve this we must handle all three possibilities. Consider this mulitway selection example:
Example 12.3: C++ source code
if(age1 == age2) { cout << "nnnnThey are the same age."; }else { if(age1 > age2) { cout << "nnnnThe first person is older."; } else { cout << "nnnnThe second person is older."; } }
- 1941 reads