Examples
Truth tables are often used to describe the results of complex boolean expressions.
Here is the truth table for the NOT operation.
x ~x true false false true x ~x Read each line as follows: true false If A is true, then ~A (NOT A) is false. false true If A is false, then ~A (NOT A) is true.
Here is the truth table for the AND operation.
A B A & B true true true true false false false true false false false false A B A & B Read each line as follows: true true true If A is true and B is true, then the result of A & B (A AND B) is true. true false false If A is true and B is false, then the result of A & B (A AND B) is false. false true false If A is false and B is true, then the result of A & B (A AND B) is false. false false false If A is false and B is false, then the result of A & B (A AND B) is false.
Here is the truth table for the OR operation.
A B A | B true true true true false true false true true false false false A B A | B Read each line as follows: true true true If A is true and B is true, then the result of A | B (A OR B) is true. true false true If A is true and B is false, then the result of A | B (A OR B) is true. false true true If A is false and B is true, then the result of A | B (A OR B) is true. false false false If A is false and B is false, then the result of A | B (A OR B) is false.
- Assume that x=10 and y=24.
Boolean Expression Value of the Expression --------------------------------------- x == 12 false y ~= 20 true x < y true y <= x false x + 20 > y true x + y >= 57 false
Read each example line aloud as follows, to understand how the results are obtained.
- The statement "x is equal to 12", is false.
- The statement "y is NOT equal to 20", is true.
- The statement "x is less than y", is true.
- The statement "y is less than or equal to x", is false.
- The statement "x plus 20 is greater than y", is true.
- The statement "x plus y is greater than or equal to 57", is false.
- Relational operators and logical operators can be combined to in very complex ways. What is the value of each expression, assuming that x=10 and y=24 again?
Boolean Expression Value of the Expression --------------------------------------------------- x + 2 == 12 true x + 2 ~= 12 false y / 3 ~= x - 2 false x < 10 | y == 24 true x < 10 & y == 24 false
Try each example in Matlab and consult the precedence chart to understand each result. - Evaluate the value of this expression if x=10 and y=24.
y >= x | x ~= 10 & y < 25
Hint: read the expression as
"y is greater than or equal to x OR x is not equal to 10 AND y is less than 25"
or
"24 is greater than or equal to 10 OR 10 is not equal to 10 AND 24 is less than 25"
If that statement is true, the result is true. If that statement is false, the result is false. Admittedly, this is hard for us to decipher the truth or falsity of either of these statements, but not for Matlab. The clear operator precedence rules ensures that it is evaluated consistently as true.
The precedence shows that the relational operations
>=
,~=
,>
have higher precedence than the logical operations|
and&
, so they execute first. The next highest precedence operation is the logical AND operation&
. Finally, the logical OR operation is evaluated. But, this all occurs in a left to right parsing of the statement. Here is the order and result of each operation that occurs in this complex expression to help you understand. Assume x=10 and y=24 again.1) y >= x | x ~= 10 & y < 25 % y >= x is true 2) true | x ~= 10 & y < 25 % x ~= 10 is false 3) true | false & y < 25 % y < 25 is true 4) true | false & true % false & true is false 5) true | false % true | false is true 5) true
The final result of this complex expression is true>.
- Assume that x=10 and y=24 again and change the first operator to the
<=
operator.Boolean Expression Value of the Expression --------------------------------------------------- x <= y | x == 10 & y < 24 true (x <= y | x == 10) & y < 24 false
Notice how the use of()
changes the order of execution of the two boolean expressions and thus changes the final result.