Expression Evaluation: Operator Associativity

What do I do when there's more than one * / + / etc?

Model 1: Operator Associativity

int x;
int y;
y = 1 + 2 * 3 + 4 * 5;
y = 1 + 2 * 3 + 4 * 5
y = 1 + 6 + 4 * 5
y = 1 + 6 + 20
y = 7 + 20
y = 27
27
Order Of Operations
Precedence Associativity
* Multiplication Left to Right
+ Addition Left to Right
= Assignment: Variable on the left hand side is given the value on right hand side, value of the right hand side is used in any further evaluation. Right to left

Critical Thinking Questions

  1. Review: Why does the multiplication ( the * symbol ) go first? Multiplication has higher precedence than addition or assignment
  2. The first operation that gets done - where is that located in the 'Order Of Operations' table? At the top
  3. What does the right-hand column say about Multiplication? "Left to right"
  4. Of the two multiplications, which one goes first and why? The left-most, because the table says to go left to right
  5. Of the two adds, which one goes first and why? The left-most, because it associates left-to-right
  6. Given that addition and multiplication both go left-to-right, why did we do the 2nd mult before the left-most add? Because multiplication is still higher precedence than addition
  7. Sometimes students feel comfortable with 'order of operations' problems that only involve arithmetic because they remember the PEMDAS rule from their math classes.
    However, mathematics doesn't have a concept of 'associativity' the way that computer programming languages do.  
    Think up / speculate / guess some reasons why programming languages' operators have a clearly defined associativity but math operators don't and discuss these reasons with your group. 
    Be prepared to discuss these wth the rest of the class.
    commutative operators don't have associativity in math because the relative order doesn't matter
    AND
    because in math you can't change things mid-expression (unlike in programming, where you can execute assignment operations and/or call functions that have side effects)

Model 2: Associativity and multiple assignments

int x = 1;
int y = 2;
x = y = 3;
x = y = 3
x y
1 2
x = 3
x y
1 3
x = 3
x y
1 3
3
x y
3 3
Order Of Operations
Precedence Associativity
* Multiplication Left to Right
+ Addition Left to Right
= Assignment: Variable on the left hand side is given the value on right hand side, value of the right hand side is used in any further evaluation. Right to left

 

  1. Notice that the right-most assignment operator is evaluated first. What does that assignment operation do? (What effect does it have?) Changes the value stored in y to be 3
  2. Does that first assignment operation change the value of x? No
  3. What does the second assignment operator doe? Changes the value stored in x to be 3
  4. Why was the right-most assignment operator evaluated first? Because assignment associates right to left, not left to right like addition and multiplication do
  5. Discuss with your group the following question: Why does the assignment operator go right to left, unlike addition and multiplication? So that we can do multiple assignments on a single line

Exercise 1: Determining Operator Evaluation Order

Goals:

  1. Apply what you've learned to solve a more realistic, open-ended problem
  2. Understand that there are several C#-specific operators (in addition to +, *, etc)

For this exercise, you must determine the order in which an expression will be evaluated.
NOTE: You do NOT need to actually do the evaluation.  You are NOT expected to know what most of these operators actually do.

The first step in figuring this out is to find a concise list of C# operators and their associativity.  The instructor has already done this for you - please download the C# language specification and look on page 114 (in section 12.4.2 Operator precedence and associativity) for a list of all C# operators, and a paragraph about operator associativity in C#.

Next, describe what order the operators in the following expression should be evaluated in. (You may assume that the expression compiles and runs.  E.g., a, b, c, and obj have been declared, etc,etc)

b = c = a[x] * 3 << 4 < 4 || 7 % 3 == 2  + obj.f(x) 

Remember, you do NOT need to actually evaluate this - you only need to figure out what order the operators (whatever they do) will be evaluated in.

 

 

First a[x] (array access)
then obj.f(x)
then *
%
+
<<
<
==
right =
left =

Credits / Copyright Info