Boolean algebra

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: ⇔ means equivalent to, meaning two expressions always produce the same value for the same variable variables, and ≡¬ mean equivalent to the negation, meaning the two expressions always produce opposite values for the same variable values.)

Operator precedence

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:

Identities

The following identities allow us to simplify expressions since all of the expressions below are equivalent to just a for all values of a.

Tautologies and contradictions

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.

Commutative properties

Both || and && are commutative (like addition and multiplication with numbers).

Distributive properties

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.)

De Morgan’s Laws

De Morgan’s Laws let us simplify expressions involving negations. You can think of them as the rules for distributing ! over || and &&.

Absorption laws

Another set of laws that let us simplify boolean expressions are the Absorption Laws. One way to think of these is to think about the short circuiting behavior of both && and ||. If a is false then a && whatever is false. And if a is true then a || whatever is true. Since the left hand sides of these laws involve a in both an && and an || expression, the b has no effect and is “absorbed”. These laws don’t depend on short circuiting but the logic of short circuiting helps explain them.

Negations

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.

Equality operators

Normally we don’t use equality operators with booleans since they are already booleans so we don’t have to compare them to literal true or false values. But these operators do have a meaning and can be useful when comparing two boolean expressions, neither of which are literals values.

Want more?

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.