1 / 69

Chapter 4: Making Decisions

Chapter 4: Making Decisions. Outline. Relational Operations If statement If If/else statement If/else if Logical operators Switch. Relational Operators.

mary
Download Presentation

Chapter 4: Making Decisions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4: Making Decisions

  2. Outline • Relational Operations • If statement • If • If/else statement • If/else if • Logical operators • Switch

  3. Relational Operators • Relational operations allow you to compare numeric and char values and determine whether one is greater, less, equal to, or not equal to another. • Operators:

  4. Relational Expressions • Boolean expressions – true or false • Examples: 12 > 5 is true 7 <= 5 is false if x is 10, then x == 10 is true, x != 8 is true, and x == 8 is false

  5. int x, y ; x = 4; y = 6; EXPRESSION VALUE x < y x + 2 < y x != y x + 3 >= y y == x y == x+2 y = x + 3

  6. int x, y ; x = 4; y = 6; EXPRESSION VALUE x < y true x + 2 < y false x != y true x + 3 >= y true y == x false y == x+2 true y = x + 3 7

  7. Relational Expressions • Can be assigned to a variable: result = x <= y; • By default, assigns 0 for false, 1 for true • Do not confuse = and == • 0 is false; any other value is true

  8. Like all C++ expressions, relational expressions are evaluated to yield a numerical result. A condition that we would interpret as true evaluates to an integer value of 1; a false condition results in an integer value of 0. Samples: cout << (3 < 4) << endl; cout << (2.0 > 3.0); Results: 1 0

  9. Outline • Relational Operations • If statement • If • If/else statement • If/else if • Logical operators • Switch

  10. ifstatement Select whether or not to execute a statement (which can be a single statement or an entire block) without the else clause TRUE expression statement FALSE

  11. Flowchart for Evaluating a Decision

  12. The if Statement • General Format: if (expression) statement; • If the expression is true, then statementis executed. • If the expressionis false, then statementis skipped.

  13. Example: Check a Car’s Mileage #include <iostream> int main ( void ) { const double LIMIT = 30000.0; //set car mileage limit double mileage = 0.0; //stores mileage entered by user cout << "Please enter the mileage recorded on the car: "; cin >> mileage; if (mileage > LIMIT) cout << "This car is over the limit."; cout << "\n\nEnd of program."; return 0; }

  14. Example: Results Output from first run: Please enter the mileage recorded on the car: 35620.8 This car is over the limit. End of program. Output from second run: Please enter the mileage recorded on the car: 25620.3 End of program.

  15. (Program Continues)

  16. Flowchart for Lines 21 and 22

  17. if statement notes • Do not place ; after (expression) • Place statement; on a separate line after (expression), indented: if (score > 90) grade = 'A'; • Be careful testing floats and doubles for equality • 0 is false; any other value is true

  18. Expanding the if Statement • To execute more than one statement as part of an if statement, enclose them in { }: if (score > 90) { grade = 'A'; cout << "Good Job!\n"; } • { } creates a block of code (like your main function)

  19. Outline • Relational Operations • If statement • If • If/else statement • If/else if • Logical operators • Switch

  20. if-elseprovides two-way selection between executing one of 2 clauses: the if clause or the else clause FALSE TRUE expression else clause if clause

  21. The if/else Statement • Provides two possible paths of execution • Performs one statement or block if the expression is true, otherwise performs another statement or block. • General Format: if (expression) statement1; // or block else statement2; // or block

  22. Use of blocks recommended if ( expression ) { } else { } “if clause” “else clause”

  23. A compound statement consists of • individual statements enclosed within braces. • Syntax: • if(expression) • { • statement1; • statement2; • statement3; • } • else • { • statement4; • statement5; • statement6; • }

  24. Exercise: mail order Assign value .25 to discount_rate and assign value 10.00 to ship_cost if purchase is over 100.00 Otherwise, assign value .15 to discount_rate and assign value 5.00 to ship_cost Either way, calculate total_bill • if(expression) • { • statement1; • statement2; • statement3; • } • else • { • statement4; • statement5; • statement6; • }

  25. These braces cannot be omitted if ( purchase > 100.00 ) { discount_rate = .25 ; ship_cost = 10.00 ; } else { discount_rate = .15 ; ship_cost = 5.00 ; } total_bill = purchase * (1.0 – discount_rate) + ship_cost ;

  26. (Program Continues)

  27. Outline • Relational Operations • If statement • If • If/else statement • If/else if • Logical operators • Switch

  28. EXACTLY 1 block of these statements will be executed. if/else ifformat (if-else chain or extended if-else) • Using nested if statements if ( Expression1 ) Statement1 else if ( Expression2 ) Statement2 . . . else if ( ExpressionN ) StatementN else Statement N+1 Used when only one condition can be true

  29. Example Marital Status Input Code Married M Single S Divorce D Widowed W

  30. Example int main ( void ) { char marital_status; //marital status code entered by user //Prompt user for marital status code cout << "Enter a marital code: " << endl; cin >> marital_status; //Displays marital status message if (marital_status == 'M') cout <<"Individual is married." << endl; else if (marital_status == 'S') cout <<"Individual is single." << endl; else if (marital_status == 'D') cout <<"Individual is divorced." << endl; else if (marital_status == 'W') cout <<"Individual is widowed." << endl; else cout << "An invalid code was entered." << endl; //used as error message return 0; }

  31. Output (Test all possible paths): Enter a marital code: D Individual is divorced. Enter a marital code: S Individual is single. Enter a marital code: M Individual is married. Enter a marital code: W Individual is widowed. Enter a marital code: m An invalid code was entered. //results in an error message

  32. Exercise: extended if statement (if-else chain): Calculate the monthly income of a salesperson by using the following commission schedule:

  33. //Calculates salesperson's income if(monthly_sales >= 50000.00) income = 375.00 + .16 * monthly_sales; else if(monthly_sales >= 40000.00) income = 350.00 + .14 * monthly_sales; else if(monthly_sales >= 30000.00) income = 325.00 + .12 * monthly_sales; else if(monthly_sales >= 20000.00) income = 300.00 + .09 * monthly_sales; else if(monthly_sales >= 10000.00) income = 250.00 + .05 * monthly_sales; else income = 200.00 + .03 * monthly_sales;

  34. int main ( void ) { //stores salesperson’s name, monthly sales and calculated income double monthly_sales = 0.0, income = 0.0; string salesperson_name; //Prompts user for salesperson's name & sales cout << "Please enter the saleperson's name: "; getline(cin, salesperson_name); cout << "Enter the value of monthly sales: " << endl; cin >> monthly_sales; //Calculates salesperson's income if (monthly_sales >= 50000.00) income = 375.00 + .16 * monthly_sales; else if (monthly_sales >= 40000.00) income = 350.00 + .14 * monthly_sales; else if (monthly_sales >= 30000.00) income = 325.00 + .12 * monthly_sales; else if (monthly_sales >= 20000.00) income = 300.00 + .09 * monthly_sales; else if (monthly_sales >= 10000.00) income = 250.00 + .05 * monthly_sales; else income = 200.00 + .03 * monthly_sales; //Displays salesperson's name & income cout << setprecision(2) << fixed <<showpoint; cout << salesperson_name << " has earned a total monthly income of $" << income << endl << " based on monthly sales of $" << monthly_sales; return 0; }

  35. Outline • Relational Operations • If statement • If • If/else statement • If/else if • Logical operators • Switch

  36. Boolean Expressions Simple boolean expression use relational and/or logical operators. 6 Relational (or comparison) Operators < <= > >= == != 3 Logical Operators ! && ||

  37. LOGICAL EXPRESSION MEANING DESCRIPTION ! p NOT p ! p is false if p is true ! p is true if p is false p && q p AND q p && q is true if both p and q are true. It is false otherwise. p || q p OR q p || q is true if either p or q or both are true. It is false otherwise.

  38. Truth Table – Logical “AND” X Y X && Y

  39. Truth Table – Logical “AND” X Y X && Y

  40. Truth Table – Logical “OR” X Y X || Y

  41. Truth Table – Logical “OR” X Y X || Y

  42. Truth Table – Logical “not” X !X

  43. Truth Table – Logical “not” X !X If the Boolean expression is true, the combined expression is false. If the Boolean expression is false, the combined expression is true.

  44. Logical Operators - examples int x = 12, y = 5, z = -4;

  45. Logical Operators - examples int x = 12, y = 5, z = -4;

  46. Write an expression for each • tax_rate is over 25% and income is less than $20000 (tax_rate > 0.25) && (income < 20000) • temperature is less than or equal to 75 or humidity is less than 70% (temperature <= 75) || (humidity < .70) • age is over 21 andage is less than 60 (age > 21) && (age < 60) • age is 21 or 22 (age == 21) || (age == 22)

  47. Example The extended if statement should be as follows to allow the user to enter either uppercase or lowercase marital codes: //Displays marital status message if (marital_status == 'M' || marital_status == 'm') cout <<"Individual is married." << endl; else if (marital_status == 'S' || marital_status == 's') cout <<"Individual is single." << endl; else if (marital_status == 'D' || marital_status == 'd') cout <<"Individual is divorced." << endl; else if (marital_status == 'W' || marital_status == 'w') cout <<"Individual is widowed." << endl; else cout << "An invalid code was entered." << endl;

More Related