control structures (multi-way selection). multi-way selection extended if-else used to select...

16
CONTROL STRUCTURES (MULTI-WAY SELECTION)

Upload: kristina-cain

Post on 01-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly

CONTROL STRUCTURES(MULTI-WAY SELECTION)

Page 2: CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly

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

Page 3: CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly

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;

Page 4: CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly

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); }

Page 5: CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly

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.

Page 6: CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly

MULTIPLE SELECTION

EXAMPLES:if (numStudents< 6) salary = hours * CLUSTER_RATE;else if (numStudents < 12) salary = hours * LIMITED_RATE; else salary = hours * FULL_RATE

Page 7: CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly

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;

Page 8: CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly

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;

Page 9: CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly

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

Page 10: CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly

MULTI-WAY SELECTION

SWITCH - SIMPLE switch (selector)

{case choice1: statement;

break;case choice2: statement;

break;case choice3: statement;

break;“ “

case choice(n): statement;}

Page 11: CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly

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);

}

Page 12: CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly

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);

}

Page 13: CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly

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.

Page 14: CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly

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;}

Page 15: CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly

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

Page 16: CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly

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