Or Truth table

A
B
A || B
F F F
F T T
T F T
T T T

Not truth table

A
!A
F T
T F

Or (written as ||) will result in true if either expression is true and is known as disjunction. Not (written as a !) will negate truth so essentially flips true to false and false to true. Some examples -

  1. if (age < 21 || age > 70) System.out.println("You get a discount train pass");
  2. if (!(age <30)) System.out.println("Your not as young as you would like!");
  3. if (!(marks < 50 && retakes < 4)) System.out.println("Do NOT do retake!");

Example 1 uses OR to say that if you are under 21 or over 70 then you get a discount rail card.

Example 2 inverts the result of age < 30 so rather than getting people younger than 30 we end up with people older!

Example 3 this negates if we need to take a retake!

We can combine these together in any combination we like to get to the end result. A common end result is known as XOR or exclusive or. The truth table for XOR is -

A
B
A XOR B
F F F
F T T
T F T
T T F

XOR is only true is one or the other expressions are true but NOT both. In logic we write XOR as -

!(A && B) && (A || B)

A
B
A && B !(A && B) A || B
!(A && B) && (A || B)
F F F T F F
F T F T T T
T F F T T T
T T T F T F

As you can see we get the same truth table. Remember that Java uses &&, || and ! to represent AND, OR and NOT.

Next summary >>

 

 
Basics
Menu
Search