Presentation is loading. Please wait.

Presentation is loading. Please wait.

DeMorgan’s Law Truth Tables Short Circuit Evaluation

Similar presentations


Presentation on theme: "DeMorgan’s Law Truth Tables Short Circuit Evaluation"— Presentation transcript:

1 DeMorgan’s Law Truth Tables Short Circuit Evaluation
Logic DeMorgan’s Law Truth Tables Short Circuit Evaluation

2 DeMorgan’s Law Let’s revisit the following code:
boolean teenager = 13 <= age && age < 20; boolean senior = age >= 65; if( !teenager && !senior )

3 DeMorgan’s Law Someone who is not a teenager AND not a senior looks like this in a truth table: Notice that in the !T && !S column only one entry is True If you’re really clever, you might even recognize that the column appears very similar to an OR truth table T S !T && !S F

4 DeMorgan’s Law Logically, we can conclude that !T && !S == !(T || S)
On a number line: T S !T && !S T || S !(T || S) F 13 20 65

5 DeMorgan’s Law Equivalence gives us a way of knowing our code is correct Also gives us an alternative way to describe a conditional statement that may be simpler Here is someone that is not a teenager: if(! (( age >= 13 ) && ( age < 20)) ) Note the extra parenthesis required since NOT would be done first! if( !(age >= 13 ) || !(age < 20) )//DeMorgan’s Law if( age < 13 || age >= 20 ) Not a teenager is also someone who is less than 13 or at least 20 In general, it is always best to use the simplest statement!

6 DeMorgan’s Law – Venn Diagrams
!(A && B) = !A || !B Similarly, !(A || B) = !A && !B

7 Converting You might have done something like this in math:
3x2 + 6 = 3(x2 + 2) You can think of DeMorgan’s Law in a similar way Here’s the process “Factor” out the ! Flip the Operator (AND/OR)

8 Converting Ex. !A && !B == !(A||B) !A && !B
!(A && B) //Factor out the ! !(A || B) //Flip the operator !A && !B == !(A||B)

9 Converting Ex. A || !B == !(!A&&B) A || !B
It appears challenging to factor out a ! In this example since A does not have one A == !!A A || !B == !!A || !B !!A || !B !(!A || B) //Factor out the ! !(!A && B) //Flip the operator A || !B == !(!A&&B)

10 Converting – You try and Verify
Ex. !A && B

11 Converting – You try and Verify
Ex. !A && B  !A && !!B  !(A && !B)  !(A || !B) A B !A !B !A && B A||!B !(A||!B) T F

12 Practice For each statement, use DeMorgan’s Law to construct 2 logical expressions (check 1 with a truth table) R = It is raining C = It is cold If it is not raining and it is not cold “Go to the park” If it is not raining and it is cold “Bring a jacket to the park” If it is raining and it is not cold “Bring an umbrella to the park” If it is raining and it is cold “Stay home”

13 Practice For each statement, use DeMorgan’s Law to construct 2 logical expressions R = It is raining C = It is cold !R && !C == !(R || C) “Go to the park” !R &&C == !(R||!C) “Bring a jacket to the park” R &&!C == !(!R||C) “Bring an umbrella to the park” R&&C == !(!R||!C) “Stay home”

14 Practice For each statement, use DeMorgan’s Law to construct 2 logical expressions (check 1 with a truth table) T = Restaurant serves Thai food C = Restaurant accepts coupons If the restaurant does not serve Thai or the restaurant does not accept coupons “Mr Joyce would prefer to eat somewhere else” If the restaurant serves Thai food or the restaurant does not accept coupons “Mr Joyce won’t complain” If the restaurant does not serve Thai food or the restaurant accepts coupons “Mr Joyce would be willing to try it” If the restaurant serves Thai food or the restaurant accepts coupons “Mr Joyce will be happy”

15 Practice For each statement, use DeMorgan’s Law to construct 2 logical expressions T = Restaurant serves Thai food C = Restaurant accepts coupons !T || !C == !(T&&C) “Mr Joyce would prefer to eat somewhere else” T || !C == !(!T&&C) “Mr Joyce won’t complain” !T || C == !(T&&!C) “Mr Joyce would be willing to try it” T || C == !(!T&&!C) “Mr Joyce will be happy”

16 Short Circuit Evaluation
Let’s look at the truth tables for AND and OR: X Y X AND Y T F X Y X OR Y T F Notice that when one value is true, it does not matter what the others are, OR will be true Notice that when one value is false, it does not matter what the others are, AND will be false

17 Java is Lazy? Java uses short circuit evaluation to evaluate conditions. This means that: if( TRUE || (other conditions) ) Java ignores everything after the || since the result will be true no matter what else is combined if( FALSE && (other conditions) ) Java ignores everything after the && since the result will be false no matter what else is combined

18 Short Circuit Evaluation
This is a useful programming feature to prevent errors from happening: Ex. if(100 / number < 10) { programming statements… } As good programmers we should check that we’re not going to divide by 0 if( (number != 0) && (100 / number < 10)) Due to short circuit evaluation, the division will not happen if number == 0 since that makes the statement number != 0 false and the rest is ignored Greenfoot example: if( character.getWorld() != null && character.isTouching( Enemy.class) )

19 Nesting statements Nesting conditions can make it easier to understand
Using && is equivalent to nesting an if statement This is thanks to short circuiting Here is the condition of a teenage boy if(gender.equals(“male”) && (13 <= age) && (age <= 19)) { programming statements… } Is equivalent to: if(gender.equals(“male”))//must be male to make it in {} if(13 <= age) && (age <= 19))//teenager Which is equivalent to: if(13 <= age) //must be >= 13 if(age <= 19)//teenager

20 Rearrange the expression
Rewrite the expression to take advantage of short circuiting if( (isTallEnough || hasParent) && hasTickets )//can ride Here are some examples to help your decision: isTallEnough = false hasParent = true hasTickets = false

21 Rearrange the expression
Rewrite the expression to take advantage of short circuiting if( ((student || senior) && hasID) || hasCoupon || isEmployee)//get discount Here are some examples to help your decision: student = false senior = true hasID = false hasCoupon = false isEmployee = true

22 What’s wrong? Rewrite the expression to fix it
String key = Greenfoot.getKey(); if((k.compareTo(“left”) == 0 || key.compareTo(“a”) == 0) && key != null) setLocation(getX() – speed_, getY());

23 Minterms Minterms give us a way to construct a logical statement from a truth table It is not always the simplest or most efficient way Minterms exploit the way && is true only when ALL statements are true || is true when any of the statements are true

24 Minterms Suppose we had the following problem to code:
Goldilocks will sneak into your home if: She is hungry and you have 3 bowls of porridge (S,M,L) She needs to rest and you have 3 chairs (S,M,L) She is tired and you have 3 beds (S,M,L) Let’s write a method: public boolean visit( boolean isHungry, boolean needsRest, boolean isTired, int bowls, int chairs, int beds)

25 Minterms Let’s list the situations where Goldilocks WILL visit
isHungry && bowls == 3 needsRest && chairs == 3 isTired && beds == 3 Each of these is what we call a minterm The result of her visit is when one of them is true (isHungry && bowls == 3) || (needsRest && chairs == 3) || (isTired && beds == 3) OR will be true when one is true

26 Java code public boolean visit( boolean isHungry, boolean needsRest, boolean isTired, int bowls, int chairs, int beds) { return (isHungry && bowls == 3) || (needsRest && chairs == 3) || (isTired && beds == 3); } Other code is possible (and even preferable) but this method will generate one of the answers

27 Ex. 2 Wayne likes to play hockey (you might say he’s “great” at it).
If it is a school night and he doesn’t have homework and he is home by 10pm (22 on a 24 hour clock), his parents will drive him to the rink. If it is not a school night his parents will drive him to the rink if he is home by 11pm (23 on a 24 hour clock) public boolean willDrive(boolean schoolNight, boolean homework, int homeBy)

28 Ex. 2 If it is a school night and he doesn’t have homework and he is home by 10pm (22 on a 24 hour clock), his parents will drive him to the rink. schooNight && !homework && homeBy <= 22 If it is not a school night his parents will drive him to the rink if he is home by 11pm (23 on a 24 hour clock) !schoolNight && homeBy <= 23 public boolean willDrive(boolean schoolNight, boolean homework, int homeBy) { return (schooNight && !homework && homeBy <= 22) || (!schoolNight && homeBy <= 23); }

29 In reality Minterms are useful for very complex logic
When you struggle to capture the logic In reality my code would have looked something like this: public boolean willDrive(boolean schoolNight, boolean homework, int homeBy) { if(schoolNight) return !homework && homeBy <= 22; } else //not a school night return homeBy <= 23;

30 Ex. 3 Before 1752 the world did not count leap years
Leap years are any year that are: Divisible by 4 and not divisible by 100 Divisible by 400 public boolean isLeap(int year)

31 Ex. 3 Before 1752 the world did not count leap years
Leap years are any year that are: Divisible by 4 and not divisible by 100 year % 4 == 0 && year %100 != 0 Divisible by 400 year % 400 == 0 public boolean isLeap(int year) { return (year >= 1752 && year % 4 == 0 && year %100 != 0) || (year >= 1752 && year % 400 == 0); }

32 Ex. 3… in reality Before 1752 the world did not count leap years
Leap years are any year that are: Divisible by 4 and not divisible by 100 year % 4 == 0 && year %100 != 0 Divisible by 400 year % 400 == 0 public boolean isLeap(int year) { if(year >=1752) if(year % 4 == 0 && year %100 != 0) return true; else if(year % 400 == 0) else return false; }

33 Ex. 4 Ice cream can be plain (0), OK (1), or amazing(2)
If the ice cream is served in a waffle cone with at least 3 toppings it’s amazing. If it’s not in a waffle cone then it can still be amazing if it costs more than $5.00 and has at least 5 toppings. If the ice cream is served in a waffle cone it can’t be plain. Ice cream with 1 topping or less is plain or if it costs less than $2 it must be plain All other combinations are considered just OK. public int iceCream(int toppings, double cost, boolean waffle)

34 Ex. 4 If the ice cream is served in a waffle cone with at least 3 toppings it’s amazing. If it’s not in a waffle cone then it can still be amazing if it costs more than $5.00 and has at least 5 toppings. waffle && toppings >= 3 !waffle && cost > 5 && toppings >= 5 If the ice cream is served in a waffle cone it can’t be plain. Ice cream with 1 topping or less is plain or if it costs less than $2 it must be plain !waffle && toppings <= 1 !waffle && cost <= 2 All other combinations are considered just OK. public int iceCream(int toppings, double cost, boolean waffle) { if(waffle && toppings >= 3 || !waffle && cost > 5 && toppings >= 5) return 2;//amazing else if(!waffle && toppings <= 1 || !waffle && cost <= 2) return 0;//plain else//every other combination return 1;//OK }


Download ppt "DeMorgan’s Law Truth Tables Short Circuit Evaluation"

Similar presentations


Ads by Google