boolean algebra boolean assertions statements that will result in true or false outcomes a > 50 =...
TRANSCRIPT
Boolean Algebra
Boolean Assertions
• Statements that will result in true or false outcomes
a > 5 0 = = b a <= b
Negating Boolean Assertions
Rewriting code for easier readability is usually why we negate Boolean assertions
if(!(x < 5))becomes
if( x > = 5)
Boolean Algebra
• Operands(values): true, false• Operators: and (&&) or( | |) not(!)
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
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
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
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.
• 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) );
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
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