Associated Topics || Dr. Math Home || Search Dr. Math

### Left Associativity

```
Date: 05/27/99 at 00:45:57
From: Mike
Subject: Left to right rule

I'm just wondering...

When we add and subtract we usually do it from left to right. For
example,  5 - 3 - 2 = ?, we do 5-3 = 2, then 2-2 = 0. Is this left to
right thing a law for mathematics or do we just use it for
consistency?

If it isn't any sort of law for math, then in the above example the
answer could be 4. Thus, as a professor once told me, this question
would contain no solution.
```

```
Date: 05/27/99 at 08:47:58
From: Doctor Rick
Subject: Re: Left to right rule

Hi, Mike, thanks for your question.

This rule ("left associativity") is not a law but a convention. Yes,
we do this for consistency. We could have chosen to associate right,
as long as we were consistent.

We could also have chosen to have no such rule at all. As the
professor said, _if_ the left associativity convention were annulled,
expressions without parentheses would be ambiguous; they would have
more than one solution, and therefore no solution. In the latter case,
we would have no way to determine how to evaluate 5-3-2, and we would
have to use parentheses to "disambiguate" the expression: we would
write either (5-3)-2 or 5-(3-2).

As it is, we do adhere to the left associativity convention, so 5-3-2
is meaningful. But you can always use parentheses to prescribe either
left or right associativity explicitly. All the left-associativity
rule does is to tell us where the parentheses belong by default (if
they are omitted).

Computer languages have precedence and associativity rules that
describe exactly how the computer is to evaluate any expression you
can throw at it. For instance, my C++ programming manual has this
chart:

Operators     Associativity   Type
---------     -------------   ----
()            left to right   parentheses
*  /  %       left to right   multiplicative
+  -          left to right   additive
<<  >>        left to right   stream insertion/extraction
<  <=  >  >=  left to right   relational
==  !=        left to right   equality
=             right to left   assignment

If you don't know C++ (or C), don't worry about what some of these
operators mean; the point is that the precedence and associativity are
prescribed. The "order of operations" is defined by the top-to-bottom
order: multiplication is done before addition, for instance. If two
operators are on the same precedence level, they are carried out
either left to right or right to left; note that in C++, there is one
operator that has right-to-left associativity.

It's harder for people to keep all these rules straight than it is for
computers, so it's good programming style to put in parentheses
wherever a human reader might possibly get confused, even though the
computer knows exactly what to do. The same goes double for math that
is written solely for human readers: use parentheses wherever they
might avoid confusion.

- Doctor Rick, The Math Forum
http://mathforum.org/dr.math/
```
Associated Topics: