CIS 160 Boolean logic

Objectives

  • Use Boolean operators in expressions
  • Complete a truth table
  • Use relational operators in expressions
  • Use logical operators in expressions

Boolean logic

  • Boolean logic was developed by George Boole in the mid 1800s
  • Boolean logic expressions evaluate to true or false
  • the primary operations are NOT, AND, and OR
  • Java uses the operator ! for NOT
  • Java uses the operator && for AND
  • Java uses the operator || for OR
  • NOT (! in Java) reverses the value of its operand ("NOT true" evaluates to false, "NOT false" evaluates to true)
  • AND (&& in Java) takes two operands; it evaluates to true if both operands are true, otherwise it evaluates to false
  • OR (|| in Java) also takes two operands; it evaluates to true if either operand is true; it evaluates to false if and only if BOTH operands are false

Truth table

AB!A!B A && B  A || B 
truetruefalsefalsetruetrue
truefalsefalsetruefalsetrue
falsetruetruefalsefalsetrue
falsefalsetruetruefalsefalse

De Morgan's laws

There are a couple of extremely useful transformations for propositional logic that seem to have been known since at least the time of Aristotle. Augustus De Morgan introduced the formal version of the following two transformation laws for propositional logic in the 1800s.

  • NOT (A OR B) is equivalent to (NOT A) AND (NOT B)
  • NOT (A AND B) is equivalent to (NOT A) OR (NOT B)
AB !(A || B)  (!A) && (!B)  !(A && B)  (!A) || (!B) 
truetruefalsefalsefalsefalse
truefalsefalsefalsetruetrue
falsetruefalsefalsetruetrue
falsefalsetruetruetruetrue

Relational operators in Java

There are six relational operators. All will evaluate to true or false.

  • < (less than)
  • <= (less than or equal)
  • > (greater than)
  • >= (greater than or equal)
  • == (equal to)
  • != (not equal to)

Program to display a truth table

DisplayTruthTable.java: displays a truth table