• Understand relational expressions.
  • Understand Boolean expressions.
  • Understand short circuting Boolean expressions.
  • Understand Boolean Algegra
  • Relational and boolean Expressions (Eck 2.5)

    Expressions – syntactically correct combination of variables, literals, operators, and method calls that evaluate to a value with a specific type. The following meta language shows where expressions are found within Java statements

    Relational expressions compare numeric operands using relational operators and result in a boolean value.

    boolean expressions perform Boolean operations on boolean operands using boolean operators.

    Relational and Boolean Operator Precedence

    The bolded descriptions in the following table are the operators discussed in this section, listed in order from highest precedence (evaluated first) to lowest precedence (evaluated last):

    Description Operators
    Unary ++ -- ! unary - unary + type-cast
    Multiply Divide * / %
    Add Subtract + -
    Relational < > <= >=
    Equality == !=
    Boolean and &&
    Boolean or ||
    Conditional ?:
    Assignment = += -= *= /= %=

    Operators on the same line have the same precedence. When operators of the same precedence are strung together in the absence of parentheses, the relational operators are evlauated left-to-right. For example, A<B>C means (A<B)>C.

    The precedence allows you to mix relational operators and boolean operators in an expression without parentheses. For example,

    x < y && y > z

    peforms the && operation after the relational operations.

    Relational Operators: > >= < <= == !=

    NOTE: = is assignment, == is comparison, assignment is an operator that can be used in expressions, but it is wise to simply use it as part of an assignment statement.

    Truth Tables

    The truth tables for boolean Operators are given as follows. Note the last column !P applies the not operator to the first column P.

    P Q && || !P
    F F F F T
    F T F T T
    T F F T F
    T T T T F

    Boolean Operators – And: && Or: || Not: !

    A boolean expression is an expression that evaluates to true or false. To support boolean expressions, Java has the primitive boolean data type and two literals true and false. For example, I can create the following variable.

    boolean b = true;

    There are several ways to construct an expression that evaluates to true or false.

    • I can use the relational operators to compare numeric operands. The following are examples.
    x < 10
    a != b
    • I can use the boolean operators on boolean operands. The following are exmaples.
    b1 && b2 || b3
    bb && !b
    • I can combine relational operators, boolean operators, and other operators.
    x < y*z || b1 && b2

    Short Circuited Boolean Operators

    Java can short-circuit the boolean operators of && and \\. Consider the following example of an && boolean expression.

    int i = 5;
    int j = 6;
    boolean b = i > j && x > y;

    In this case Java evaluates the expression i > j && x > y from left to right. We know that i > j is false since i is 5 and j is 6. We also know that false && anything is false. Thus we know that b is false without having to evaluate x > y. Java does this exact short circuit – does not evaluate the x > y. Likewise Java will perform a similar short circuit for logical \\ when the leading expression is true. The following is an example.

    b = j > i  || x > y; // x > y not evaluated

    For the most part, this short-circuiting is rather innocuous; however, if the subsequent short-circuited expressions have side effects that are executed your algorithm may have a difficult to discover bug. We will demonstrate this in Assignment Expressions.

    The != and ! Operators

    Note that the not operator ! is similar to !=

    George Boole

    Our computers are a massive Boolean expression fabricated in silicon. We should be thankful for George Boole, whose work gives us Boolean logic.

    George Boole was a 19th century English mathematician, philosopher and logician.
    George was from from a modest family, whose Dad loved math and passed his passion to George. George wrote several books. Here are two of his books available via the Gutenberg organization.

    Boolean Algebra

    Boolean algebra can be helpful at times to simplify a complicated boolean expression. When we study while loops, we will learn that they keep going as long as an expression is true. Sometimes we know when we want to terminate a while loop, which means we know the NOT of the expression needed to keep the while loop going. De Morgan’s law can be useful in transforming a termination boolean expression to a keep going boolean expression.

    Boolean Algebra

    DeMorgan’s Law Example

    This example applies DeMorgan’s Law to an loop, which we study in Control Flow. In this example, you want to quit a while loop when the user presses Enter with nothing or the command quit. The basic structure for a while loop is the following.

    while (<exp-is-true>) { keep looping }

    When the Boolean expression

    userInput.equals(“”) || userInput.equals(Quit) 

    becomes true you want to stop the while loop.

    The following are steps of applying DeMorgan’s law.

    1. !(userInput.equals(“”) || userInput.equals(“Quit”))
    2. (!userInput.equals(“”) && !userInput.equals(“Quit”))

    This results in the following while loop.

    while (!userInput.equals(“”) && !userInput.equals(Quit)) {

    The following is a picture of Augustus DeMorgan.

    Augustus De Morgan

    Tags: expression