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: = and ≠ below, outside of the Java expressions, are used in their mathematical, not Java, sense.)

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

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.

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.