Examples

  1. Truth tables are often used to describe the results of complex boolean expressions.

    Here is the truth table for the NOT operation.

    x~x
    truefalse
    falsetrue
    x~xRead each line as follows:
    truefalseIf A is true, then ~A (NOT A) is false.
    falsetrueIf A is false, then ~A (NOT A) is true.


  2. Here is the truth table for the AND operation.

    ABA & B
    truetruetrue
    truefalsefalse
    falsetruefalse
    falsefalsefalse
    ABA & BRead each line as follows:
    truetruetrueIf A is true and B is true, then the result of A & B (A AND B) is true.
    truefalsefalseIf A is true and B is false, then the result of A & B (A AND B) is false.
    falsetruefalseIf A is false and B is true, then the result of A & B (A AND B) is false.
    falsefalsefalseIf A is false and B is false, then the result of A & B (A AND B) is false.


  3. Here is the truth table for the OR operation.

    ABA | B
    truetruetrue
    truefalsetrue
    falsetruetrue
    falsefalsefalse
    ABA | BRead each line as follows:
    truetruetrueIf A is true and B is true, then the result of A | B (A OR B) is true.
    truefalsetrueIf A is true and B is false, then the result of A | B (A OR B) is true.
    falsetruetrueIf A is false and B is true, then the result of A | B (A OR B) is true.
    falsefalsefalseIf A is false and B is false, then the result of A | B (A OR B) is false.


  4. 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.

    1. The statement "x is equal to 12", is false.
    2. The statement "y is NOT equal to 20", is true.
    3. The statement "x is less than y", is true.
    4. The statement "y is less than or equal to x", is false.
    5. The statement "x plus 20 is greater than y", is true.
    6. The statement "x plus y is greater than or equal to 57", is false.
  5. 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.
  6. 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>.

  7. 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.