control structures (multi-way selection). multi-way selection extended if-else used to select...
TRANSCRIPT
CONTROL STRUCTURES(MULTI-WAY SELECTION)
MULTI-WAY SELECTION
EXTENDED IF-ELSE Used to select exactly one task out of multiple tasks
(or possibly none) Unlike a series of “ifs” evaluation stops once one
relational expression evaluates as true (others are skipped)
Always does on less evaluation than a series of “ifs” Well planned implementation provides for efficiency –
most common choice is listed first
MULTIPLE SELECTION
EXTENDED IF-ELSE - SIMPLE if ( relational expression(s) )
statement;
else
if ( relational expression(s) )
statement;
else
if ( relational expression(s) )
statement;
else
statement;
MULTI-WAY SELECTION
EXTENED IF-ELSE - COMPOUND if ( relational expression(s) )
{statement;statement(s);
}else if ( relational expression(s) ) { statement; statement(s); } else if ( relational expression(s) ) { statement; statement(s); }
MULTI-WAY SELECTION
RULES1. First statement(s) will be executed if and only if
the evaluation of the relational expression(s) is/are true.
2. Second statement(s) will be executed if and only if the evaluation of the first relational expression(s)
is/are false and if and only if the evaluation of the second relational expression(s) is/are true.
3. Same progression as 1 and 2 for all levels that exist.
Note: This process does not have to end with two, three or four selections, but can continue for as many selections as needed.
MULTIPLE SELECTION
EXAMPLES:if (numStudents< 6) salary = hours * CLUSTER_RATE;else if (numStudents < 12) salary = hours * LIMITED_RATE; else salary = hours * FULL_RATE
MULTIPLE SELECTION
EXAMPLES:if (temperature <= 10) cout << “Go Bowling” << endl;else if (temperature <= 32) cout << “Go Skiing” << endl; else if (temperature <= 70) cout << “Play Football” << endl; else if (temperature <= 85) { cout << “Play Tennis” << endl; cout << “Wear white!” << endl; } else cout << “Go Swimming” << endl;
if (temperature > 85) cout << “Go Swimming” << endl;else if (temperature > 70) {
cout << “Play Tennis” << endl; cout << “Wear white!” << endl;
} else if (temperature > 32) cout << “Play Football” << end; else if (temperature > 10) cout << “Go Skiing” << endl;
else cout << “Go Bowling” << endl;
MULTI-WAY SELECTION
NESTED IF-ELSE Situations where action is dependent upon two or more different conditions Examples:
if (gender == ‘M’) if (age < MAX_AGE) cout << “Male and under 67.” << endl; else cout << “Male and 67 or over.” << endl;else if (age < MAX_AGE) cout << “Female and under 67.” << endl; else cout << “Female and 67 or over.” << endl;
if (savings >= DOLLAR_LIMIT) if (vacation >= VACATION_LIMIT) cout << “Trip to Hawaii” << endl; else cout << “Vacation at local ski resort.” << endl;else cout << “Go camping in mountains.” << endl;
MULTI-WAY SELECTION
SWITCH Alternate form of multi-way selection Decision can only be made on integral or
enumerated data types (bool, char, int)Does not work with strings or floating point
numbers
Only works when testing for specific valuesDoes not work with ranges
MULTI-WAY SELECTION
SWITCH - SIMPLE switch (selector)
{case choice1: statement;
break;case choice2: statement;
break;case choice3: statement;
break;“ “
case choice(n): statement;}
MULTI-WAY SELECTION
SWITCH – WITH DEFAULT switch (selector)
{case choice1: statement;
break;case choice2: statement;
break;“ “
case choice(n): statement;break;
default: statement;statement(s);
}
MULTIPLE SELECTION
SWITCH – COMPOUND switch (selector)
{case choice1: statement;
statement(s);break;
case choice2: statement;statemant(s);break;
“ “case choice(n): statement;
statement(s);break;
default: statement;statement(s);
}
MULTIPLE SELECTION
RULES1. Selector may only be an integral data type (int,
char, bool, or enumerated).2. Corresponding statement(s) is/are executed if
choice is equal to the selector.3. After corresponding statement(s) are executed
if break is not present additional case statements are executed until a break is found or end of structure is found.
4. If no choice is equal to the selector, the switch is exited unless a default is available.
MULTIPLE SELECTION EXAMPLES:
switch(category){ case 1: cout << “Category One” << endl; break;
case 2: cout << “Category Two” << endl; break;
: :
case n: cout << “Category n” << endl; break;}
MULTI-WAY SELECTION EXAMPLE:
int num1, num2, answer;char choice;
cout << "Enter two numbers, separated by a space: ";cin >> num1 >> num2;
cout << endl << endl;cout << "Perform which operation on the Numbers?" << endl;cout << " a - Add" << endl;cout << " s - Subtract" << endl;cout << " m - Mulitply" << endl;cout << " d - Divide" << endl;cout << "Enter your choice from above: " << endl;cin >> choice; choice = tolower (choice);switch (choice){
case 'a‘: answer = num1 + num2;cout << "The sum is " << answer << endl;break;
case 's‘: answer = num1 - num2;cout << "The difference is " << answer <<
endl;break;
case 'm‘: answer = num1 * num2;cout << "The product is " << answer << endl;break;
case 'd‘: answer = num1 / num2;cout << "The quotient is " << answer <<
endl;break;
default : cout << "Invalid choice entered!" << endl;
} // end switch
MULTI-WAY SELECTION
EXAMPLE:char answer;
cout << "Would you like to continue [Y] or [N]?" << endl;
cin >> answer;
switch (answer)
{
case 'Y':
case 'y‘: cout << "You chose yes" << endl;
cout << "Way to go." << endl;
break;
case 'N':
case 'n‘: cout << "You chose no" << endl;
cout << "Not very positive." << endl;
break;
default : cout << "You were suppose to choose Y or N." << endl;
cout << "What happened?" << endl;
} // end switch