boolean algebra boolean assertions statements that will result in true or false outcomes a > 50 =...

11
Boolean Algebra

Upload: laurence-dixon

Post on 19-Jan-2016

220 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Boolean Algebra Boolean Assertions Statements that will result in true or false outcomes a > 50 = = ba

Boolean Algebra

Page 2: Boolean Algebra Boolean Assertions Statements that will result in true or false outcomes a > 50 = = ba

Boolean Assertions

• Statements that will result in true or false outcomes

a > 5 0 = = b a <= b

Page 3: Boolean Algebra Boolean Assertions Statements that will result in true or false outcomes a > 50 = = ba

Negating Boolean Assertions

Rewriting code for easier readability is usually why we negate Boolean assertions

if(!(x < 5))becomes

if( x > = 5)

Page 4: Boolean Algebra Boolean Assertions Statements that will result in true or false outcomes a > 50 = = ba

Boolean Algebra

• Operands(values): true, false• Operators: and (&&) or( | |) not(!)

Page 5: Boolean Algebra Boolean Assertions Statements that will result in true or false outcomes a > 50 = = ba

DeMorgan’s Laws

• not(A or B) = not A and not B!(A || B) = !A && !B

• not(A and B) = not A or not B!(A && B) = !A || ! B

Page 6: Boolean Algebra Boolean Assertions Statements that will result in true or false outcomes a > 50 = = ba

Application of DeMorgan’s Law“Craps”

• If you roll a 7 or 11 on the first roll, you win• If you roll a 2, 3, or 12 on the first roll, you lose• Otherwise on subsequent rolls you want to

roll your original number before you roll a 7 to win

Page 7: Boolean Algebra Boolean Assertions Statements that will result in true or false outcomes a > 50 = = ba

Here is the truth table that proves the first DeMorgan’s Law. not(A or B) = not A and not B

!(A || B) = !A && !B

A B !(A||B) !A !B !A&&!B

true true false false false false

true false false false true false

false true false true false false

false false true true true True

Page 8: Boolean Algebra Boolean Assertions Statements that will result in true or false outcomes a > 50 = = ba

Following is the truth table that proves the second DeMorgan's Law.not(A and B) = not A or not B

!(A && B) = !A || ! B

A B !(A&&B) !A !B !A||!B

true true false false false false

true false true false true true

false true true true false true

false false true true true true

Notice that columns with the titles ! (A && B) and ! A || ! B result in the same answers.

Page 9: Boolean Algebra Boolean Assertions Statements that will result in true or false outcomes a > 50 = = ba

• initial roll – compare• subsequent rolls until you won or lost the game:

do –while with the sentinel: while( !((sum == point) || (sum == 7)) );

while( (sum != point) && (sum != 7) );

Page 10: Boolean Algebra Boolean Assertions Statements that will result in true or false outcomes a > 50 = = ba

Proving Demorgan’s LawI

A B not(A or B) not A not B not A and not B

1 1 0 0 0 0

1 0 0 0 1 0

0 1 0 1 0 0

0 0 1 1 1 1

Page 11: Boolean Algebra Boolean Assertions Statements that will result in true or false outcomes a > 50 = = ba

Proving Demorgan’s LawII

A B not(A and B) not A not B not A or not B

1 1

1 0

0 1

0 0