DeMorgan’s Law Truth Tables Short Circuit Evaluation

Slides:



Advertisements
Similar presentations
Selection Control Structures Chapter 5: Selection Asserting Java © Rick Mercer.
Advertisements

Logic & program control part 3: Compound selection structures.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Lecture 7 Topics –Boolean Algebra 1. Logic and Bits Operation Computers represent information by bit A bit has two possible values, namely zero and one.
4. Computer Maths and Logic 4.2 Boolean Logic Simplifying Boolean Expressions.
Introduction to Programming Using C Basic Logic. 2 Contents Conditional programming If statement Relational operators Compound statements Loops While.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
APS105 Deciding 1. Comparing 2 Relational Expressions Relational expression: –Compare two values (or values of variables) Examples: x > 5 income < expenses.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Discussion 4 eecs 183 Hannah Westra.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Chapter 4 Repetition Statements (loops)
Basics programming concepts-2
Boolean expressions and if-else statements
Georgia Institute of Technology
Handling Exceptionally Sticky Problems
Chapter 4: Making Decisions.
Section 7.1 Logical Operators
More important details More fun Part 3
Lecture 3- Decision Structures
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
Boolean Expressions and If
Boolean algebra Last time we talked about Boolean functions, Boolean expressions, and truth tables. Today we’ll learn how to how use Boolean algebra to.
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Lesson 9: "if-else-if" and Conditional Logic
Equations and Inequalities
Intro to Logic Logical statements evaluate to one of two things:
Barb Ericson Georgia Institute of Technology August 2005
Conditionals & Boolean Expressions
MSIS 655 Advanced Business Applications Programming
More Selections BIS1523 – Lecture 9.
Conditionals & Boolean Expressions
Propositional Calculus: Boolean Algebra and Simplification
ICS 3U Tuesday, September 21st.
Conditional Statements
Conditions and Ifs BIS1523 – Lecture 8.
Georgia Institute of Technology
Building Java Programs
Java Programming Loops
Understanding the Three Basic Structures
Computers in the real world Objectives
Java Programming Control Structures Part 1
Flow Control Statements
Problem Solving Designing Algorithms.
Concept 8 Inductive Reasoning.
Conditional Logic Presentation Name Course Name
The System.exit() Method
Control Structure Chapter 3.
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
The Java switch Statement
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
SSEA Computer Science: Track A
Building Java Programs
Chapter 3: Selection Structures: Making Decisions
Evaluating Boolean expressions
Analysis of Logic Circuits Example 1
Handling Exceptionally Sticky Problems
Unit 3: Variables in Java
ECE 352 Digital System Fundamentals
Decision Structures if, if/else conditions
Logical and Rule-Based Reasoning Part I
Conditionals and Loops
The IF Revisited A few more things Copyright © Curt Hill.
Boolean Logic.
ECE 352 Digital System Fundamentals
The IF Revisited A few more things Copyright © Curt Hill.
The boolean type and boolean operators
Presentation transcript:

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

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

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

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

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!

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

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)

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

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)

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

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

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”

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”

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”

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”

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

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

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) )

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

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

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

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());

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

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)

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

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

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)

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); }

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;

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)

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); }

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

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)

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 }