Vous êtes sur la page 1sur 6

Conditional Logic

Sunday, January 27, 2013 5:57 PM

Basics Conditional logic uses conditional keywords (AND, OR, NOT, XOR) to form an expression. The most basic form of conditional logic is when you are given two binary numbers and a conditional logic keyword. AND (&&): returns true (1) when both numbers are 1 - if not, it will return false (0) OR (||, but sometimes +): returns 1 if either number is a 1, if not, it will return 0 NOT (! or ~): uses only one number, and it simply reverses the value (1 to 0, 0 to 1) XOR: aka exclusive or, will only return 1 if one value of the other is true. If both are true, it will return 0. NOR: neither one or the other; only returns true when both expressions are false. NAND: (not and) acts like and, but returns 1 when both numbers are 0 A tautology is an expression that is always true. For example, the statement B or not B (B || !B) will always be true. Therefore, if you come across this statement in a proof, you can cancel it out. It will always be true no matter what. Note: A' means A is false. Ex: 1 OR 0 = True = 1 Ex: T AND F = False = 0 Ex: T XOR T = False = 0 Truth Tables Truth tables are used to evaluate bigger expressions. Sometimes you will be given only the table and asked to find the expression of it. Sometimes you will be given a big expression and be asked to find an equivalent expression. In all these cases, truth tables help. To make a truth table, list all the variables (A, B, C, whatever can have value of 1 or 0) and the result in every possible case. Ex: Make a truth table for the expression (A || B) || !(A && B) A 1 1 0 0 B 1 0 1 0 Result 1 1 1 1

For this example, I will explain how I filled the table. First, I filled in all the possible combinations of A and B, which were 1 1, 1 0, 0 1, 0 0. Then, I tested each of the combinations. (1 || 1) || !(1 && 1) = 1 || !1 = 1 || 0 = 1 (1 || 0) || !(1 && 0) = 1 || !0 = 1 || 1 = 1 (0 || 1) || !(0 && 1) = 1 || !0 = 1 || 1 = 1 (0 || 0) || !(0 && 0) = 0 || !0 = 0 || 1 = 1 However, you might have noticed something. In all cases but the last, the first expression (A || B) evaluates to 1. Since the overall expression is (A || B) || !(A && B), only one of these expressions
Computer Science Page 1

evaluates to 1. Since the overall expression is (A || B) || !(A && B), only one of these expressions has to be true. Either (A || B) or !(A && B) has to be true and the whole thing evaluates to 1. Since (A || B) evaluates to 1 for the first the cases, you don't have to evaluate the rest of the expression: you know it comes out to be true! Don't go overboard with this. Make sure the logic is right before you take shortcuts like this. The longer version of the table looks like this: A T T F F B T F T F A || B T T T F A && B !(A && B) (A || B) || !(A && B) T F F F F T T T T T T T

De Morgan's Law De Morgan's Law can be complicated to explain, but basically it involves the distribution of NOT statements in an expression. When you distribute a NOT, each item in an expression is affected. 0s become 1s, 1s become 0s, ANDs become ORs, and ORs become ANDs. Ex: !(A && B) --> !A || !B Ex: !(A || B) --> !A && !B Here's the proof, if you need it: !(A && B) = !A || !B A T T F F B T F T F (A && B) !(A && B) !A T F F F F T T T F F T T !B F T F T !A || !B F T T T

Since the !(A && B) column has the same output as the !A || !B column, they are logical equivalents. Here's the reverse proof: !(A || B) = !A && !B A T T F F B T F T F (A || B) !(A || B) !A T T T F F F F T F F T T !B F T F T !A && !B F F F T

Since the !(A || B) column has the same output as the !A && !B column, they are logical equivalents. Boolean Algebra Conditional logic, also known as boolean logic or is actually a form of algebra, known as boolean algebra. Just like regular algebra, boolean algebra has specific properties that will help you to solve logical proofs. Here are some of the most important properties: Commutative property of addition:
Computer Science Page 2

Commutative property of addition: A+B=B+A A or B = B or A Commutative property of multiplication: AB = BA A and B = B and A Associative property of addition: A + (B + C) = (A + B) + C A or (B or C) = (A or B) or C Associative property of multiplication: A(BC) = (AB)C A and (B and C) = (A and B) and C Distributive property - AND over OR: A(B + C) = AB + AC A and (B or C) = (A and B) or (A and C) Distributive property - OR over AND: A + BC = (A + B)(A + C) A or (B and C) = (A or B) and (A or C) Some rules allow things to "undo" each other or to get rid of one part of the expression. For example: Anything ORed with its inverse is always true B || !B = TRUE Anything ANDed with itself is just itself A && A = A Anything ANDed with TRUe is just itself A && 1 = A Anything ORed with FALSE is just itself A || 0 = A Some rules allow for "absorption" - the condensation of a whole expression into just one condition. 2nd Absorption Law: A or (A and B) = A Proof: A or (A and B) (A and TRUE) or (A and B) A and (TRUE or B) A and TRUE A 1st Absorption Law: A and (A or B) = A Proof: A and (A or B) (A and A) or (A and B) A or (A and B) A (by the second absorption law) 3rd Absorption Law: A or (not A and B) = A or B Proof: A or (not A and B) (A or not A) and (A or B) TRUE and (A or B) (A or B) Finding equivalents
Computer Science Page 3

Finding equivalents Sometimes you will be given an expression and asked to find an equivalent. This will usually involve the use of de Morgan's law. Ex: What expression is logically equivalent to the following expression: !((x>y) || (x%3 !=0) Work: Apply de Morgan's law in pieces: !((x>y) || (x%3 !=0) !(x>y) && !(x%3 != 0) (x <= y) && !(x%3 != 0) Answer: (x <= y) && !(x%3 != 0) Ex: The following Boolean expression, (not A and C) or (B and not C) or not(B or C) Is equivalent to what? Work: Apply de Morgan's law: (not A and C) or (B and not C) or not(B or C) (not A and C) or (B and not C) or (not B and not C) Apply the distributive property (not A and C) or ((B or not B) and not C) Tautology (not A and C) or (TRUE and not C) Answer: (not A and C) or not C Ex: What is the Boolean expression that matches the given truth table? A F F F F T T T T B F F T T F F T T C F T F T F T F T OUTPUT T F T F T T F F

Work: Add together expressions that made the output true: A'B'C' + A'BC' + AB'C' + AB'C Separate into new expressions. Try to keep like terms together: (A'B'C' + A'BC') + (AB'C' + AB'C) Use the distributive property A'C'(B' + B) + AB'(C' + C) Now use an identity to cancel some things. In this case, use any item OR'd with its inverse is true. A'C' + AB'

Computer Science Page 4

Use the commutative property (if needed) to make sure your answer matches one of the choices. AB' + A'C' Answer: AB' + A'C' Ex: What is the Boolean expression that matches the given truth table? A F F F F T T T T B F F T T F F T T C F T F T F T F T Output F T T T F T T T

Work: In this case, we have to do a bit of simple critical thinking. Looking at the output, we see that true is always returned when B or C is true. Therefore, though there may be other expressions that fit this table, the most simple expression is B OR C. Answer: B OR C Ex: Determine the logical expression that corresponds to the OUTPUT of the following truth table: A F F F F T T T T B F F T T F F T T C F T F T F T F T OUTPUT T F T F T F T F

Work: List all the values that made the output true. A'B'C', A'BC', AB'C', ABC' What's the common trend among all of these? C is always false. No matter what happens to the other values, the simplest expression that can define this truth table is that C is always false. Answer: not C Ex: Determine the logical expression that corresponds to the following truth table A F F F B F F T C F T F Value F F F
Computer Science Page 5

F F T T T T

T T F F T T

F T F T F T

F T F T F T

Work: First, list all the values that return true. A'BC, AB'C, ABC What's the first thing they have in common? C is always true. We also notice that either A or B (and in one case both) are true. So we AND our expressions together and get (A || B) && C. This is commutative, so we could also write it as (A && C) || (B && C) Answer: (A && C) || (B && C)

Computer Science Page 6

Vous aimerez peut-être aussi