# Lab 3: Experiment 7

## Operator Associativity

Operator precedence only gives part of the story, though, when determining what order to apply operators. What happens when all of your operators have the same precedence. For example,

`8 - 4 - 2`
If the left-most subtraction is performed first, then the expression evaluates to
`(8 - 4) - 2 = 4 - 2 = 2`
If the right-most subtraction is performed first, then the expression evaluates to
`8 - (4 - 2) = 8 - 2 = 6`
We see again that the order in which operators are applied determines the value of the expression. However, some characteristic other than precedence must be used when all the operators in the expression have the same precedence level.

This new characteristic is called operator associativity. Associativity determines if the operator associates to the left or right. Using the example above, if subtraction were left associative, then the compiler implicitly parenthesizes from left to right:

`(8 - 4) - 2`
If subtraction were right associative, then the compiler implicitly parenthesizes right to left:
`8 - (4 - 2)`
Keep in mind that we write the unparenthesized expression. The compiler implicitly adds the parentheses, and it can't be ambiguous about it's choice.

So, which is it? Try it out. Modify your program to print the result of the parenthesis-free expression. Compile and execute your program.

Question #3.7.1: Is subtraction left or right associative?

Test out division, too:

`8 / 4 / 2`

Question #3.7.2: Write the equation with explicit parentheses if division were left associative. What does it evaluate to?

Question #3.7.3: Write the equation with explicit parentheses if division were right associative. What does it evaluate to?

Now test it. Modify your program to print the result of the parenthesis-free expression. Compile and execute your program.

Question #3.7.4: Is division left or right associative?

`8 + 4 + 2`

Question #3.7.5: Write the equation with explicit parentheses if addition is left associative. What does it evaluate to?

Question #3.7.6: Write the equation with explicit parentheses if addition is right associative. What does it evaluate to?

So, for some operations it doesn't appear to matter. In some situations, the associativity for addition does matter, but we won't encounter these situations.

## Everything to the Left

You should discover that all of the arithmetic, relational, and logic operators are left associative. The mathematicians insist on this for subtraction and division. To make things consistant, C++ makes all of the arithmetic operators left associative.

In fact, it should be evident that the insertion operator `<<` is also left-associative. Consider this output statement:

`cout << "i is " << i << endl;`
There are three objects that are printed to the screen, a string, an integer, and a newline.

Question #3.7.7: What is printed first? What is printed second? What is printed last?

Whatever is done first should have the first set of implicit parentheses around it.

Question #3.7.8: How would you parenthesize the output statement?

Any C++ textbook or reference should give you a complete list of operator precedence and associativity.

## Actually, Not Everything

Some operators do associate to the right, the most common being the assignment operator (see Experiment #9).

## Terminology

left associative, operator associativity, right associative
Back to the Lab Exercise  |  Forward to the Next Experiment