Truth tables

Edit on Github instead

Truth tables are tools to generalize a logic expression, even as its arguments change.

Here's the process behind making a truth table for the && operator:

boolean a = true;
boolean b = true;
a && b;                     // true

a = true;
b = false;
a && b;                     // false

a = false;
b = false;
a && b;                     // false

a = false, b = true;
a && b;                     // false

If we put that information into a table, we get a truth table:

a b (a && b)
true true true
true false false
false true false
false false false

From the truth table, it's easier to see that (a && b) is true only if both a and b are true.

Here's the truth table for (a || b):

a b (a || b)
true true true
true false true
false true true
false false false

(a || b) is true more often than (a && b), as the || operator is more forgiving; only one of a and b are true in order for the entire expression to be true.

Every expression has a truth table. For example, here's one for (!a || b && c):

a b c (!a || b && c)
true true true true
true true false false
true false true false
true false false false
false true true true
false true false true
false false true true
false false false false

Suggest changes

Related links

URL Topic Source