Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly."— Presentation transcript:

1 CONTROL STRUCTURES (MULTI-WAY SELECTION)

2 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

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

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

5 MULTI-WAY SELECTION  RULES 1.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.

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

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

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

9 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 values  Does not work with ranges

10 MULTI-WAY SELECTION  SWITCH - SIMPLE  switch (selector) { case choice1:statement; break; case choice2:statement; break; case choice3:statement; break;“ case choice(n):statement; }

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

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

13 MULTIPLE SELECTION  RULES 1.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.

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

15 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

16 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


Download ppt "CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly."

Similar presentations


Ads by Google