
One of the drawbacks of two way selection is that we can only consider two choices. But what do you do if you have more than two choices. Consider the following which has four choices:
if age equal to 18 you can now voteelse if age equal to 39 you are middle aged else if age equal to 65 you can consider retirement else your age is unimportant
You get an appropriate message depending on the value of age. The last item is referred to as the default. If the age is not equal to 18, 39 or 65 you get the default message. In some situations there is no default action. Consider:
if age equal to 18 you can now voteelse if age equal to 39 you are middle aged else if age equal to 65 you can consider retirement
The last if then else control structure has no "else". It's implied "else do nothing". Without the default the multiway selection could be written as a series of "if then without the else" structures. Consider:
if age equal to l8 you can now vote if age equal to 39 you are middle aged if age equal to 65 you can consider retirement
We have shown two ways to accomplish multiway selection. The choice of using nested if then else control structures or a series of if then control structures is decided on the existence of a default action (you must use nested if then else) or programmer preference if there is not a default action (you may use nested if then else or a series of if then control structures).
- 1757 reads