1 / 33

CSCI 142 Decision Logic

CSCI 142 Decision Logic. Arithmetic Operators. Multiplication * Division / Remainder % Addition + Subtraction -. These have precedence!. Use parentheses to change precedence or make your intentions clear. Division and Remainder.

virgil
Download Presentation

CSCI 142 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 142Decision Logic

  2. Arithmetic Operators Multiplication* Division / Remainder % Addition + Subtraction - These haveprecedence! Use parentheses to change precedence or make your intentions clear

  3. Division and Remainder • If both operands to the division operator (/) are integers, the result is an integer • The fractional part is discarded 14 / 3equals 8 / 12equals • The remainder operator (%) returns the remainder after dividingthe second operand into the first 14 % 3equals 8 % 12equals

  4. Practice! • 10 + 3 • 3 * 5 • 5 - 2 * 3 • 3 / 2 • 5 % 3 • 2 * 3 + 4 / 5 • 3 * 4 / (6 - 5 % 2) • 1 + 2 * 3 / 4 • 5 % 6 - 2 • 3 + 2 * (2 – 5) / 4 All expressions containing a relational operator will result in either a true or false answer only.

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

  6. Understanding Precedence Use parentheses to change precedence or make your intentions clear

  7. Practice! • 10 + 3 < 5 * 2 • 3 * 5 == 12 • 5 - 2 * 3 > 6 == false • 7 > 3 * 4 / 2 • 1 + 2 * 3 / 4 < 5 % 6 - 2 • 5 != 10 / 2 • ‘b’ <= ‘a’ • 3 + 2 * 2 >= 10 • 7 % 3 != 4 • false == true All expressions containing a relational operator will result in either a true or false answer only.

  8. Logical Operators 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.

  9. Using the NOT Operator • Negates the result of any booleanexpression • !true  • !(2 < 3)  • !(2 >= 1 && 3 == 4)  • 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?

  10. Understanding Precedence You can always use parentheses to change precedence or make your intentions clear

  11. Practice! • 3 > 2 && 6 <= 5 • !(5 <= 3) • 8 == 4 * 2 || 7 < 5 • 10 < 25 && 6 > 5 + 1 • 3 >= -6 || 1 > -1 && 9 < 8

  12. Writing Boolean Conditions • To pass a course, a student must have an average test score of at least 75. • Write the condition using a variable test. test >= 75

  13. Writing Boolean Conditions • 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

  14. Writing Boolean Conditions • Only people with insurance code 150 who are over 65 years old receive a discount. • Write the condition using the variables code and age. code == 150 && age > 65

  15. Writing Boolean Conditions • Employees with job codes 34 and 67 will receive a raise. • Write the condition using the variable code. code == 34 || code == 67

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

  17. 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

  18. 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

  19. Flowchart Symbols • start/stop oval • Process/calculation • input/output • Decision or loop condition

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

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

  22. If it’s sunny, I will go to the park. Later, I will study. If it’s sunny then go to park study F T Sunny? Go to park Study Flowchart Pseudocode

  23. method name Printing Strings • The ConsoleProgram class has methods for printing text. • The println method accepts a String argument println ("Whatever you are, be a good one."); information provided to the method (argument)

  24. Example if (isSunny) { println(“Go to park”); } println(“Study”);

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

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

  27. If it’s sunny, I’ll go to the park. If it’s not, I’ll play a game. Later, I’ll study. if sunny is true then go to park else play a game study F T Sunny? Play a game Go to park Study Flowchart Pseudocode

  28. if…else Example if (isSunny) { println(“Go to park”); } else { println(“Play a game”); } println(“Study”);

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

  30. If it’s sunny, I’ll go to the park. If the grass is dry, I’ll have a picnic. If not, I’ll sit sadly in my car. If it’s not sunny, I’ll stay home and play a game. if sunny is true then go to park if grass dry is true then have a picnic else sit sadly in car else stay home play a game F T Sunny? Go to park Stay home Grass dry? Play a game F T Have picnic Sit sadly in car Pseudocode Flowchart

  31. Nested If Example if (isSunny) { if (grassIsDry) { println(“Picnic!"); } else { println(“Sit in car"); }} else { println(“Play a game"); }

  32. 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.

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

More Related