1 / 28

CSCI 142 Introduction to Object Oriented Programming Decision Logic

CSCI 142 Introduction to Object Oriented Programming Decision Logic. Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to. Relational Operators. == > >= < <= !=. Not ! And && Or ||.

Télécharger la présentation

CSCI 142 Introduction to Object Oriented Programming Decision Logic

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. CSCI 142Introduction to Object Oriented ProgrammingDecision Logic

  2. Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to Relational Operators == > >= < <= !=

  3. Not ! And && Or || Reverses the truth value of a condition; false becomes true and true becomes false All conditions must be true for the compound condition to be true Only one of the conditions needs to be true for the compound condition to be true. Logical Operators

  4. Using the NOT Operator • Negates the result of any boolean expression • Should be avoided when possible if (!(x > 5)) { x++;} if (x<=5) { x++;} How could you rewrite this so that the NOT operator is avoided?

  5. Example of Logical Operators • To pass a course, a student must have an average test score of at least 75 and an average project score of at least 35. • Write the condition using the variables test and proj. test >= 75 && proj >= 35

  6. Example of Logical Operators • Only people living in the state of Michigan who are over 65 years old receive a discount. • Write the condition using the variables state and age. state == “Michigan” && age > 65

  7. Example of Logical Operators • Only employees with job codes of 34 and 67 will receive a raise. • Write the condition using the variable code. code == 34 || code == 67

  8. Understanding Precedence • Operations have higher and lower precedence • The order in which you use operators makes a difference • You can always use parentheses to change precedence or make your intentions clear

  9. 10 + 3 < 5 * 2 3 * 5 == 12 5 - 2 * 3 > 6 == false false == !true 3 > 2 && 6 <= 5 8 == 4 * 2 || 7 < 5 7 > 3 * 4 / 2 5 != 10/2 3 + 2 * 2 < 30 % 20 ‘b’ < ‘a’ 10 < 25 && 6 > 5 + 1 8 > 5 && (6 < 5 || 3 < 4) Practice! All expressions containing a relational operator will result in either a true or false answer only.

  10. Programming Structures • Sequence • Program instructions are processed in the order in which they appear in the program • Selection • Allows the program to make a decision and then select the appropriate action • Repetition (looping or iteration) • One or more instructions is repeated a specified number of times or until a condition is met

  11. Selection Structure • Used to make a decision • Evaluates condition to determine path • Condition results in true (yes) or false (no) • If the condition is true, the program performs one set of tasks • If the condition is false, the program may or may not perform a different set of tasks

  12. Design tools • Pseudocode- Programmers use a list of tasks that must be accomplished to help them plan a program’s logic • Flowchart- The programmer writes the steps in diagram form as a series of shapes connected by arrows

  13. Flowchart Symbols • start/stop oval • process rectangle • input/output parallelogram • selection/repetition diamond • symbols are connected by flowlines

  14. If condition is true then Perform task A Perform task B if Statement Algorithm F T Cond A B Flowchart Pseudocode

  15. if Statement Syntax if (condition) { instructions when the condition is true } other statements…

  16. if Statement Example intnum = 5; if (num > 5) { num++; } println(num);

  17. Example if (hoursWorked > 40) { regularPay = 40 * rate; //Time and a half for hours over 40 overTimePay = (hoursWorked – 40) * 1.5 * rate; //Print pay println(“Regular pay is “ + regularPay); println(“Overtime pay is “ + overTimePay); } //if structure ends here

  18. if condition is true then perform task A else perform task B perform task C if…else Algorithm F T Cond B A C Flowchart Pseudocode

  19. if…else Syntax if (condition) { instructions when the condition is true } else { instructions when the condition is false } other instructions…

  20. if…else Example double grade = readDouble(“Enter grade:”); if (grade >= 2.0) { println(“You passed!”); } else { println(“You failed.”); }

  21. Nesting if statements • “Nested if” • if statement inside another if statement • Useful when multiple conditions must be met before some action can occur

  22. Nested If Example Write a selection structure that assigns a sales tax rate to double tax. The tax rate is determined by the code stored in int code. Codes of 1 and 3 represent a 4% rate; a code of 2 represents a 5% rate. All other codes represent a 2% rate.

  23. Nested If Example if (code == 1 || code == 3) tax = .04; else { if (code == 2) tax = .05; else tax = .02;} Curly braces are optional when there is a single statement following an if or an else.

  24. Nested If Example 2 A salesperson inputs their code and their sales amount. Write a selection structure that assigns a bonus based on the following logic: If the code is 1 and the salesperson sold at least $10,000, then the bonus is $500; otherwise these salespeople receive $200. If the code is 2 or 3 and the salesperson sold at least $20,000, then the bonus is $600; otherwise these salespeople receive $550. All others receive $150.

  25. if (code == 1) { if (sales >= 10000) bonus = 500; else bonus = 200; } else if (code == 2 || code == 3) { if (sales >= 20000) bonus = 600; else bonus = 550; } else { bonus = 150; }

  26. Using the Switch Statement • Switch statement • Tests a single variable against a series of exact integer or character values • Keywords • switch - starts the structure and is followed immediately by a test expression enclosed in parentheses • case - is followed by one of the possible values for the test expression and a colon • break - optionally terminates a switch structure at the end of each case • default - optionally is used prior to any action that should occur if the test variable does not match any case

  27. int year = readInt(“Enter your year:”); switch(year) { case 1: println(“Freshman”); break; case 2: println(“Sophomore”); break; case 3: println(“Junior”); break; case 4: println(“Senior”); break; default: println(“Invalid year”); }

More Related