Skip to main content

Subsection 2.1.5 Operator Precedence and Parentheses

“Jim is tall or Joe is short” is a statement (that happens to be the or of two more basic statements). Since it is a statement, we can turn around and use it as an operand to form an even larger statement. Combining it with the simple statement “Mary is young”, we get the more complex statement:

“Mary is young and Jim is tall or Joe is short”.

We now have two operators. But how do we know the order in which they should be applied? Just looking at the statement (without knowing that we started with “Jim is tall or Joe is short”), it is possible that we meant:

“Mary is young” and “Jim is tall or Joe is short”

Or we could have meant:

“Mary is young and Jim is tall” or “Joe is short”

Notice that our analogy with arithmetic continues here. Suppose we write:

\(3 + 5 * 2 \)

We could mean:

\((3 + 5) * 2 \) , which would be 16.

Or we could mean:

\(3 + (5 * 2) \) , which would be 13.

We will solve this problem with logical expressions in exactly the same way we solve it with arithmetic expressions:

  • We will assign a precedence level to each of our operators. All operators with higher precedence get applied before any operators of lower precedence do. So, in arithmetic, we know that \(-3 + 5 \) is \(2 \) because we apply the minus sign to the value \(3 \) before we add \(5 \text{.}\) We don’t first add \(3 \) to \(5 \) and then apply the minus sign to get \(-8 \) .Minus has higher precedence than addition does. We will define precedence levels for our logical operators after we have described them in detail in the next few sections.

  • We can use parentheses whenever the arbitrary precedence levels don’t do what we need them to do (or, often, when we’re not sure what they are going to do and we don’t want to take a chance of getting it wrong). So we can write either of these:

Mary is young and (Jim is tall or Joe is short).

(Mary is young and Jim is tall) or Joe is short.

English Aside

Of course, English doesn’t get to use parentheses willy nilly. Good writers try to get the same effect with careful punctuation. And sometimes signs exploit layout and spacing. A lot of the time, writers count on the ability of the rest of us to figure out what makes sense. See the next two examples.

Can you figure out what a tourist’s options are at this café (in Cartagena, Spain)?

Some restaurants seem to get it, though. Can you see how our local Russian restaurant has indicated ands and ors and shown the correct groupings of them? (The ors here correspond to the logical operator that we’re going to call exclusive-or. You may have exactly one of the items in the list.)

Legal documents must solve this problem. For some reason, they don’t like just to use parentheses, which would make it all very easy. So they have to try other things. Here is an example that uses blocks on the page to describe one’s choices for documents that can be submitted along with a US I-9 form (that certifies that one is legally allowed to work in the US).

But it’s not always possible, and sometimes it’s next to impossible, to figure out what was meant.

Can you figure out how to add parentheses to this menu fragment to make it clear what a hungry eater’s choices are:

Big Idea

One of the big reasons that we’ve defined our logical language in the way we have is that it doesn’t force readers (be they people or machines) to guess what writers meant.

Exercises Exercises

1.

Let’s assume that a word, like “pancakes”, corresponds to the claim, “You can have pancakes.” Then consider this expression:

pancakes or bacon and eggs

Which of these parenthesizations is much more likely to correspond to what a menu-writer meant:

  1. pancakes or (bacon and eggs)

    or

  2. (pancakes or bacon) and eggs

    or

Answer.
Solution.
The one with pancakes all by themselves is correct. We use our knowledge of breakfast foods to realize that bacon and eggs is one standard breakfast, while pancakes is another.