The following material is not all covered in BHSawesome and isn’t explicitly called out in the AP CSA curriculum. However, the exam may include questions that can be solved very quickly if you are comfortable manipulating boolean expressions and which will otherwise will require tediously plugging in values and tracing code. If you’re shooting for a 5 on the exam, you may want to spend some time with boolean algebra. (Note: = and ≠ below, outside of the Java expressions, are used in their mathematical, not Java, sense.)
The three logical operators in Java have an order of precedence just like arithmetic operators and ()
s can be used to group expressions. The order of precedence between the logical operators and parens is, from highest to lowest:
()
!
&&
||
The following identities allow us to simplify expressions since all of the expressions below are equivalent to just a
for all values of a
.
true && a
false || a
a && a
a || a
!!a
A tautology is a statement that is always true and a contradiction is a statement that is always false. Below are one important instance of each.
Tautology (always true): a || !a
Contradiction (always false): a && !a
Both ||
and &&
are commutative (like addition and multiplication with numbers).
a || b = b || a
a && b = b && a
Both ||
and &&
distribute over the other (which is unlike addition and multiplication of numbers where multiplication distributes over addition but not the other way around.)
a || b && c = (a || b) && (a || c)
a && (b || c) = a && b || a && c
De Morgan’s Laws let us simplify expressions involving negations. You can think of them as the rules for distributing !
over ||
and &&
.
!(a || b) = !a && !b
!(a && b) = !a || !b
The following pairs of expressions always have oppositive values, for all values of a
and b
. I.e. each is the negation of the other.
a ≠ !a
a == b ≠ a != b
a < b ≠ a >= b
a < b ≠ a >= b
If you are particularly interested in understanding and simplfying boolean expressions algebraically you may also find this page on boolean expressions of two variables useful.