1 / 63

Decision Structures

Decision Structures. Michele Co CS 101-E. Today. if statements Logical expressions Truth tables Boolean algebra DeMorgan’s laws Boolean expressions Relational Operators Equality Ordering Expressions vs. Statements. Background. So far, sequential code We need more control…

Download Presentation

Decision Structures

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. Decision Structures Michele Co CS 101-E

  2. Today • if statements • Logical expressions • Truth tables • Boolean algebra • DeMorgan’s laws • Boolean expressions • Relational Operators • Equality • Ordering • Expressions vs. Statements

  3. Background • So far, sequential code • We need more control… • Java constructs to control execution of statements • if • if-else • if-else-if Relies on logical expressions

  4. Conditional Statements

  5. Conditional Constructs • if statements • if • if-else • if-else-if • switch statements

  6. Expression true false Action Basic if statement • Syntax if (Expression) Action • If the Expression is true then execute Action • Action is either a single statement or a group of statements within braces • For us, it will always be a group of statements within braces

  7. Are the numbers out of order Rearrange value1 and value2 < value1 value2 to put their values in the proper order false true int rememberValue1 = value1 value1 = value2 value2 = rememberValue1 The numbers were rearranged into the proper order The numbers were initially in order The numbers are in order If semantics

  8. What an if statement executes • An if statement executes the next block of code • A block is either: • A single statement without curly brackets: if (a == b) System.out.println (“a==b!!!”); • A number of statements enclosed by curly brackets: if (a == b) { System.out.print (“a”); System.out.print (“==”); System.out.print (“b”); System.out.println (“!!!”); }

  9. What is the Output? int m = 5; int n = 10; if (m < n) ++m; ++n; System.out.println(" m = " + m + " n = “ + n);

  10. Code Demo: AverageScore.java

  11. Expression false true Action1 Action2 The if-else statement • Syntax if(Expression) Action1elseAction2 • If Expression is true then execute Action1otherwise execute Action2 • The actions are either a single statement or a list of statements within braces

  12. Finding the maximum of two values System.out.print("Enter an integer number: "); int value1 = stdin.nextInt(); System.out.print("Enter another integer number: "); int value2 = stdin.nextInt(); int maximum; if (value1 < value2) { // is value2 larger? maximum = value2; // yes: value2 is larger } else { // (value1 >= value2) maximum = value1; // no: value2 is not larger } System.out.println("The maximum of " + value1 + " and " + value2 + " is " + maximum);

  13. Is value2 larger than value1 Yes, it is . So value2 is larger than value1. In No, its not. So value1 this case, maximum is is at least as large as set to value2 value2. In this case, maximum is set to value1 value1 < value2 false true maximum = value2 maximum = value1 Either case, maximum is set correctly Finding the maximum of two values

  14. Code Demo: Division.java

  15. If-then-else precedence if (number != 0) if (number > 0) System.out.println("positive"); else System.out.println("negative"); Which if does this else refer to?

  16. Code Demo: TestResults.java

  17. Logical Expressions

  18. Logical Expressions • The branch of mathematics dealing with logical expressions is Boolean algebra • Developed by the British mathematician George Boole • Logical expression values • logical true • logical false

  19. Logical Operators • Logical and • && • Logical or • || • Logical not • !

  20. Truth Tables • Formal specification for an operator • Lists all combinations of operand values and results for each combination

  21. p q p and q False False False False True False True False False True True True Logical AND (&&) Short circuit evaluation for logical AND: if left hand side is false, then whole expression evaluates to false

  22. p q p or q False False False False True True True False True True True True Logical OR (||) Short circuit evaluation for logical OR: if left hand side is true, then whole expression evaluates to true

  23. p not p False True True False Logical NOT (!)

  24. p q p and qnot (p and q) False False FalseTrue False True FalseTrue True False False True True True TrueFalse Boolean Algebra • Complex logical expressions can be formed by combining simpler logical expressions

  25. (not p) orpqp and qnot (p and q)( not p)(not q)(not q) False False False True True True True False True False True True False True True False False True False True True True True True False False False False DeMorgan’s Laws (1) • not (p and q) equals (not p) or (not q)

  26. (not p) andp q p or q not (p or q) ( not p) (not q)(not q) False False False True True True True False True True False True False False True False True False False True False True True True False False False False DeMorgan’s Laws (2) • not (p or q) equals (not p) and (not q)

  27. DeMorgan’s Laws • ! (a && b) == (! a) || (! b) • ! (a || b) == (! a) && (! b)

  28. Sidewalk chalk guy • Source: http://www.gprime.net/images/sidewalkchalkguy/

  29. Boolean Expressions

  30. Java’s boolean type • Has 2 literal constants • true • false • Operators • && • || • !

  31. Defining boolean variables • Local boolean variables are uninitialized by default boolean isWhitespace; boolean receivedAcknowledgement = true; boolean haveFoundMissingLink = false;

  32. Assignment vs. comparison • = is the assignment operator • Consider: int x; x = 5; • == is the comparison operator • Consider: int x = 5; System.out.println (x == 5); System.out.println (x == 6); • Prints out true, false

  33. Equality Operators • == • returns true if operands have the same value • != • returns true if operands have different values Works with all sorts of values

  34. Evaluating boolean expressions • Suppose boolean p = true; boolean q = false; boolean r = true; boolean s = false; • What is the value of p p && s !s p == q q q != r p && r r == s q || s q != s

  35. Evaluating boolean expressions • Suppose int i = 1; int j = 2; int k = 2; char c = '#'; char d = '%'; char e = '#'; • What is the value of j == ki != k i == jj != k c == ed != e c == d c != e

  36. A bit of humor…

  37. Ordering operators • Java provides ordering operators for the primitive types • <, >, <=, and >= • They correspond to mathematical operators of <, >, ≤, and ≥ • Together the equality and ordering operators are known as the relational operators

  38. Relational Operators Summary

  39. Evaluating Boolean Expressions

  40. Evaluation boolean expressions • Suppose int i = 1; int j = 2; int k = 2; • What is the value of i < j j < k i <= k j >= k i >= k

  41. Unicode values • Character comparisons are based on their Unicode values • Characters ‘0’, ‘1’, … ‘9’ have expected order • Character ‘0’ has the encoding 48 • Character ‘1’ has the encoding 49, and so on. • Upper case Latin letters ‘A’, ‘B’, … ‘Z’ have expected order • Character ‘A’ has the encoding 65, character ‘B’ has the encoding 66, and so on. • Lower case Latin letters ‘a’, ‘b’, … ‘z’ have expected order • Character ‘a’ has the encoding 97, `b’ 98, etc.

  42. Evaluation boolean expressions • Suppose char c = '2'; char d = '3'; char e = '2'; • What is the value of c < d c < e c <= e d >= e c >= e

  43. Operator precedence revisited • Highest to lowest • Parentheses () • Unary operators - • Multiplicative operators * / % • Additive operators + - • Relational ordering <,>, <=, >= • Relational equality ==, != • Logical and && • Logical or || • Assignment =

  44. Expressions vs. Statements • Statement • Single command for Java to execute • Examples • System.out.println (“hello world”); • int x = 4; • ++x; • Expression • Returns a value (does not have semi-colon) • Note the difference between the following: • ++i is an expression • ++i; is a statement

  45. Sand Castles

  46. Evaluating Boolean Expressions Precedence In-Class Exercise

  47. More Complex Boolean Expressions w/Precedence • 7 < 4 < 1 • 3 - -4 != 6 % 11 • 4 < 9 == false • 7 > 2 != false • 2 == 2 == 1 • 2 – 6 < 6 – 2 • 6 % 5 <= 5 + 7

  48. What Order of Evaluation? What Result? • 3 < 7 && 7 < 14 • 2 > 6 && 6 == 14 • false || false && true • 4 == 6 || 6 != 4 • true || false && true && false • ! ( 4 != 8 != true) • ! false || ! true • 8 / 3 >= 1 == ! true • false || true && true || false

  49. Expressions and Floating Point Precision FloatTest.java

  50. Take care with floating-point values • Consider double a = 1; double b = 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 double c = .9999999999999999; • Two true expressions! c == b b != a • Two false expressions! a == b b != c • Problem lies with the finite precision of the floating-point types • Instead with the ordering operators for closeness

More Related