Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to Logic Logical statements evaluate to one of two things:

Similar presentations


Presentation on theme: "Intro to Logic Logical statements evaluate to one of two things:"— Presentation transcript:

1 Intro to Logic Logical statements evaluate to one of two things:
True False The three basic logical operators are: OR AND NOT Another common logical operator for programmer is the exclusive or, usually abbreviated to be XOR

2 Evaluating Statements Using OR
In Logic, OR is true when at least one value is true Ex. (Mr. Joyce went to SFU) OR (his hair is on fire) (Hopefully) only one of those statements is true, making the entire statement When evaluating we usually replace the statements with T or F to shorten it down.

3 Your Turn Will the following statements be true or false?
(Horses have tails) OR (Fish have fur) (Dogs can fly) OR (Birds can fly) (Fish can swim) OR (Cats meow) (Pigs can fly) OR (horses can fly) T OR F = T F OR T = T T OR T = T F OR F = F

4 Evaluating Statements Using AND
In Logic, AND is true when ALL values are true Ex. (Mr. Joyce went to SFU) AND (Mr. Joyce was a lifeguard) Both of those statements are true, making the entire statement true.

5 Your Turn Will the following statements be true or false?
(Horses have tails) AND (Fish have fur) (Dogs can fly) AND (Birds can fly) (Fish can swim) AND (Cats meow) (Pigs can fly) AND (horses can fly) T AND F = F F AND T = F T AND T = T F AND F = F

6 That was fun… NOT! In logic, NOT ‘flips’ the value of a statement
NOT True = False NOT False = True Sometimes NOT is a convenient alternative Ex. An odd number ends in 1 OR 3 OR 5 OR 7 OR 9 An odd number is NOT divisible by 2

7 Exclusive OR In logic, XOR is true when one value is true and the other is not Usually when we say ‘or’ in English we mean exclusive OR Ex. Do you want to go to the store ‘or’ see a movie

8 Your Turn Will the following statements be true or false?
(Horses have tails) XOR (Fish have fur) (Dogs can fly) XOR (Birds can fly) (Fish can swim) XOR (Cats meow) (Pigs can fly) XOR (horses can fly) T XOR F = T F XOR T = T T XOR T = F F XOR F = F

9 Truth Tables One tool people use when they study logic is a truth table Truth tables exhaust all possibilities for a series of logical statements and operators 2 statements will have 4 rows 3 statements will have 8 rows 4 statements will have 16 rows n statements will have 2n rows

10 Or – Truth Table X Y X OR Y T F
The first row reads: “When X is true and Y is T, X OR Y is True” All possible combinations of T/F are present If you replace T with 1 and F with 0, you also exhaust all of the 2 bit binary values

11 Fill in the AND Truth Table
X Y X AND Y T F T F F F

12 Compound Statements Logic wouldn’t get us very far if all we had were a few operators. It is the combination of operators that make logic such a powerful tool Logical statements, like math are evaluated left to right, however, AND has higher precedence than OR (like x has higher precedence than +) Evaluate: (Dogs Fly) OR (Cats meow) AND (Cows moo) (Dogs Fly) OR (T AND T) (Dogs Fly) OR (T) F OR T T AND done first Result

13 Compound Statements If you’re not sure which about the order,
replace OR with + replace AND with x Decide the order you would evaluate the math statement that results. Ex. (Cows Moo) OR (Dogs Bark) AND (Cats fly) (Cows Moo) (Dogs Bark) x (Cats fly) Second First

14 All OR, evaluate left to right
Your Turn Evaluate: (Dogs fly) OR (Pigs meow) OR (Turtles have shells) (F OR F) OR (Turtles have shells) (F) OR (Turtles have shells) (F) OR (T) T All OR, evaluate left to right Result

15 All AND, evaluate left to right
Your Turn Evaluate: (Dogs bark) AND (Pigs Oink) AND (Cats meow) (T AND T) AND (Cats meow) (T) AND (Cats meow) (T) AND (T) T All AND, evaluate left to right Result

16 Your Turn Evaluate: (Pigs fly) AND (Dogs meow) OR (Ducks Quack)
(F AND F) OR (Ducks Quack) (F) OR (Ducks Quack) (F) OR (T) T AND done first Result

17 What about NOT? NOT has the highest precedence off all logical operators, NOT will always be evaluated first Ex. NOT (Pigs fly) AND (Dogs Bark) NOT (F) AND (Dogs Bark) (T) AND (Dogs Bark) (T) AND (T) T

18 Parenthesis change order
What if you need to force OR to be evaluated first? Again think about how you could make + evaluate before x in math 5 x = = 33 But, 5 x (6 + 3) = 5 x 9 = 45 Similarly, (Dogs bark) OR (Pigs fly) AND (Fish run) = T ((Dogs bark) OR (Pigs fly)) AND (Fish run) = F Parenthesis change order

19 Summary Operators: Expressions are evaluated left to right Precedence:
AND OR NOT Expressions are evaluated left to right Precedence: NOT first AND second OR last Parenthesis can be used, like in math, to change order of evaluation

20 Exercises Evaluate the following expressions, showing your work at each step (Bears have teeth) AND (Fish have legs) (Pigs Oink) OR (Dogs meow) T AND F F T OR F T

21 Exercises cont’ (Cats fly) AND (Dogs bark) OR (Fish walk) F AND T OR

22 Exercises cont’ NOT (Pigs fly) AND (Birds fly) OR (Cats have gills)
(Fish walk) (T) AND (Birds fly) OR (Fish walk) (T) AND (T) OR (Fish walk) T OR (Fish walk) T OR F T

23 Exercises cont’ (Pigs fly) OR NOT (Birds fly) AND (Cats have gills)

24 Exercises cont’ (Cats meow) AND ((Pigs fly) OR (Dogs Bark))
( (T) OR (F) ) (Cats meow) AND ( T ) (T) AND (T) (T)

25 Exercises cont’ ((Pigs have tails) OR NOT(Dogs have tails)) AND (NOT(Fish have tails) OR (Cats have tails)) ((Pigs have tails) OR NOT(Dogs have tails)) AND (NOT(Fish have tails) OR (Cats have tails)) ((Pigs have tails) OR NOT(T)) AND (NOT(Fish have tails) OR (Cats have tails)) ((Pigs have tails) OR (F)) AND (NOT(Fish have tails) OR (Cats have tails)) ( (T) OR (F)) AND (NOT(Fish have tails) OR (Cats have tails)) (T) AND (NOT(Fish have tails) OR (Cats have tails)) (T) AND (NOT(F) OR (Cats have tails)) (T) AND ( (T) OR (Cats have tails)) (T) AND ( (T) OR (T)) (T) AND (T) T

26 Tip of the iceberg… if is only the beginning of conditional statements. How would you determine if someone is a teenager? The logical operators we learned about earlier are also part of the Java laguage.

27 Logical Operators in Java
Earlier you learned that the primary Logic operators are: AND OR NOT In Java, they have the symbols: AND && OR || NOT ! IMPORTANT! & and | mean something else in Java (binary logic operators), your program will compile fine, but produce unexpected results, also known as a logic error. Make sure you use && and ||, we will not be using & or |

28 Watch out for Teenagers!
A teenager is some who’s age is between 13 and 19 (inclusive) We could also say they are: greater than or equal to 13 less than or equal to 19. The if statement in java would become: if(( age >= 13 ) && ( age <= 19 )) { this person is a teenager! } In math when a value is between 13 and 19, we write: 13 <= x <= 19 It can be written in Java as (13 <= x && x <= 19) to resemble math

29 Compound if statements
if statements that involve the logical operators AND, OR, and NOT are called compound statements because they require more than one evaluation Here is someone that is not a teenager: if(! (( age >= 13 ) && ( age <= 19 )) ) Note the extra parenthesis required since NOT would be done first! if( (age < 13 ) || (age > 19 ) ) A teenage is also someone who is less than 13 or greater than 19 In general, it is always best to use the simplest statement!

30 Just how bad can it get? How could you write a statement for someone that is not a teenager and is also not a senior citizen (65 and older)? teenager: Greater than or equal to 13 and Less than or Equal to 19 (age >= 13) && (age <= 19) senior: Greater than or equal to 65 (age >= 65) Putting them together… brace yourself if( !( (age >= 13) && (age <= 19) ) && !(age >= 65) )

31 boolean The previous example is to illustrate a point
Conditional statements can get VERY messy Java has a base type that can make the code a lot easier to read. boolean is the type that stores true and false It is named after George Boole who invented Boolean algebra (what you learned a bit about a the beginning of the unit) Let’s try the previous example again.

32 Not a Teenager And Not a Senior
Greater than or equal to 13 and Less than or Equal to 19 (age >= 13) && (age <= 19) senior: Greater than or equal to 65 (age >= 65) This time we’ll use a boolean variable to record the value instead: boolean teenager = (age >= 13) && (age <=19); boolean senior = (age >= 65); if( !teenager && !senior ) Much easier to manage!

33 Exercise if statements
Assume that i = 1, j = 2, k = 3 and m = 2. What does each of the following statements print? System.out.println( i == 1 ); System.out.println( j == 3 ); System.out.println( ( i >= 1 ) && ( j < 4 ) ); System.out.println( ( m <= 99 ) & ( k < m ) ); System.out.println( ( j >= i ) || ( k == m ) ); System.out.println( ( k + m < j ) | ( 3 - j >= k ) ); System.out.println( !( k > m ) );

34 De Morgan’s Law Compare the truth tables for: T F T F F F T T T T T T
X Y NOT( X AND Y) T F X Y NOT X OR NOT Y T F F F T T T T T T The two statements are logically equivalent so they can be interchanged

35 De Morgan’s Law Compare the truth tables for: T F T F F F F F F F T T
X Y NOT( X OR Y) T F X Y NOT X AND NOT Y T F F F F F F F T T The two statements are logically equivalent so they can be interchanged

36 De Morgan’s Law NOT ( X AND Y ) = (NOT X) OR (NOT Y)
The two statements below are equivalent by De Morgan’s Law NOT( (Dogs bark) AND (Pigs fly) ) = T (NOT (Dogs bark)) OR (NOT (Pigs fly)) = T NOT ( X OR Y ) = (NOT X) AND (NOT Y) NOT( (Dogs bark) OR (Pigs fly) ) = F (NOT (Dogs bark)) AND (NOT (Pigs fly)) = F De Morgan’s Law can also be verified with Venn Diagrams

37 Using De Morgan’s Law Programmers use De Morgan’s Law to simplify decisions when possible In the previous example we had code that said: if( !teenager && ! senior ) According to De Morgan’s Law, that can be rewritten as: if( !( teenager || senior ) ) Now there are only two conditions to evaluate rather than 3.

38 Using De Morgan’s Law Rewrite using De Morgan’s Law:
!( x < 5 ) && !( y >= 7 ) !( a == b ) || !( g != 5 ) !( ( x <= 8 ) && ( y > 4 ) ) !( ( i > 4 ) || ( j <= 6 ) ) ! ( ( x <5 ) || ( y >=7 ) ) ! ( ( a == b ) && ( g != 5 ) ) ! ( x <= 8 ) || ! ( y > 4 ) ! ( i > 4 ) && ! ( j <=6 )

39 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

40 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

41 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

42 Nesting statements Nesting conditions can make it easier to follow than using many && or || operators Here is the condition of a teenage boy if(gender == “male” && (13 <= age) && (age <= 19)) { programming statements… } Is equivalent to: if(gender == “male”)//must be male to make it in {} if(13 <= age) && (age <= 19))//teenager

43 Nested if/else Finding the minimum of three values, trace by hand, value1 = 10, value 2 = 9, and value3 = 5: if( value1 < value2 )//value1 is the minimum { if( value1 < value3 )//value1 beats them all System.out.println( value1 + “ is the minimum ); } else System.out.println( value3 + “ is the minimum ); else//value2 is the minimum if( value2 < value3 )//value2 beats them all System.out.println( value2 + “ is the minimum );


Download ppt "Intro to Logic Logical statements evaluate to one of two things:"

Similar presentations


Ads by Google