This section contains 132 words (approx. 1 page at 300 words per page) |
Multiway selection is a term that is relevant to programming in computer science. Languages such as C++ employ multiway selection. Specifically, multiway selection is a style of code writing that allows the testing of a variable against a list of possible values. The different values will affect the variable in different ways. Thus, multiway selection allows a programmer to assess the influence of values on the desirable or undesirable function of a variable, and to program in various options for a given variable.
Multiway selection operates by the use of what is termed the switch statement. A switch statement is what permits many possibilities to be presented, along with the criteria for their selection.
The following is an example of multiway selection:
- switch (lottery win) {
- case (10):
cout << "Get 5 more lottery tickets"<< endl; - break;
- case (25):
cout << "Get expensive meal in a restaurant" << endl; - break;
- case (100):
cout << "Weekend at local Bed and Breakfast" << endl; - break;
- case (1000):
cout << "Cruise around the world" << endl; - break;
- case (10000):
cout << "Stop writing and retire" << endl; - break;
- default:
cout << "Put winnings in the bank." << endl; - } // end of switch
As illustrated above, a multiway selection invokes a default action as well as a number of actions that are dependent on the changeable nature of the variable (in this case the size of the lottery winning). In a real case, the computer would look at the value of the data point and then jump to appropriate case in the switch statement. After executing the 'cout' statement a 'break' statement would be encountered. The computer would then jump to the end of the switch statement. If the data point does not match any of the listed cases, the default clause would be invoked, if present. A default condition is not mandatory. But it is often useful.
The multiway selection builds more flexibility into a program. This is particularly important when the variable is inherently complex in its behavior. Invoking a single option for a variable that is capable of multiple behaviors would be inappropriate.
This section contains 132 words (approx. 1 page at 300 words per page) |