- Forward


Logical Operators
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • Definitions:
    • An operator is a symbol indicating that an operation is to be performed on one or more operands
    • An operand can be a variable, literal, or expression
  • Number of Operands:
    • A unary operator has one operand
    • A binary operator has two operands
  • Boolean Values:
    • Either true (T) or false (F)
Going Further: Operator Notations
Back SMYC Forward
  • Infix Notation for Binary Operators:
    • Operand Operator Operand
  • Postfix Notation for Binary Operators:
    • Operand Operand Operator
  • Prefix Notation for Binary Operators:
    • Operator Operand Operand
Going Further: Operator Notations (cont.)
Back SMYC Forward
  • Other Names:
    • Prefix Notation is sometimes called Polish Notation
    • Postfix Notation is sometimes called Reverse Polish Notation
  • Resulting Humor(?):
    • /imgs
      (Courtesy of xkcd)
Logical Operators
Back SMYC Forward
  • Operands:
    • Booleans
  • Result:
    • A Boolean
  • Unary Operations:
    • NOT - The result is the other possible value
  • Binary Operations:
    • AND - The result is true if both operands are true; otherwise the result is false
    • OR - The result is false if both operands are false; otherwise the result is true
    • XOR - The result is true if the operands are different; otherwise the results is false
Truth Tables
Back SMYC Forward
truth_tables
The boolean Type in Java
Back SMYC Forward
  • Recall:
    • boolean is an atomic/primitive/fundamental type
  • Literals:
    • true
    • false
Logical Operators in Java
Back SMYC Forward
  • Binary Operators:
    • AND: &
    • OR: |
    • XOR: ^
  • Unary Operators:
    • NOT: !
boolean Variables in Java
Back SMYC Forward

Example

// Declare a boolean variable named ok boolean ok; // Assign the value true to ok ok = true;
Examples of Expressions Involving Logical Operators in Java
Back SMYC Forward
  • Involving Literals:
    • true & true
    • true | false
    • !false
    • true | (false & true)
  • Involving Variables (Declared Elsewhere):
    • enrolled & paid
    • student | professor
    • !old
Short-Circuiting
Back SMYC Forward
  • Some Observations:
    • If the left-side operand of AND is false then the result must be false (regardless of the right-side operand)
    • If the left-side operand of OR is true then the result must be true (regardless of the right-side operand)
  • An Implication:
    • It is not always necessary to evaluate the right-side operand of a logical operator
Short-Circuiting (cont.)
Back SMYC Forward
  • The Short-Circuiting Operators in Java:
    • AND: &&
    • OR: ||
  • Short-Circuiting in Java:
    • false && x evaluates to false (sometimes called the simplification rule) so x isn't evaluated
    • true || x evaluates to true (sometimes called the addition rule) so x isn't evaluated
  • Common Practice:
    • The short-circuiting versions are almost always used
Gaining Experience with Short Circuiting
Back SMYC Forward
  • Will the right-side operand be evaluated in the following expressions?
    • (true) || (false && true)
    • (false) || (false && true)
    • (true) && (false && true)
    • (false) && (false && true)
  • Will the right-most operand be evaluated in the following expressions?
    • (true && true) || (false && true)
    • (false && true) || (false && true)
    • (true || true) && (false && true)
    • (false || true) && (false && true)
Short-Circuiting (cont.)
Back SMYC Forward
  • Question:
    • Why doesn't Java have a ^^ operator?
  • The (Hopefully Obvious) Answer:
    • XOR can't be short-circuited; the right-side operand must be evaluated
There's Always More to Learn
Back -