Expression Evaluation: Operator Precedence

When evaluating an expression, what do I do first? second? third?

Motivation and Goal

tl;dr: You're going to learn the rules that programming languages use to evaluate expressions.
We're going to start simple but by the end of these exercises you should be able to correctly determine the order of operations for this expression:

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

We're going to learn the rules for how expressions are evaluated in the C# programming language. (We're going to look at C#, but the rules are pretty much the same for any other programming language).  An expression could be something like (I'm going to assume that int a; int b; int c; have already been declared):

a = 1;

or something like:

a = 2 * b + c / 4;

or something like:

Console.WriteLine( 2 * b + c / 4);

The goal is for you to be able to determine the order of operations for any C# expression.

Model 1: Order Of Operations

Arithmetic, Assignment Operators

Example A

int y;
y = 3 * 3 + 2;
y = 3 * 3 + 2
y = 9 + 2
y = 9 + 2
y = 11
y = 11
11
Order Of Operations
* Multiplication
+ Addition
= 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.

Example B

int y;
y = 3 + 3 * 2;
y = 3 + 3 * 2
y = 3 + 6
y = 3 + 6
y = 9
y = 9
9

Critical Thinking Questions

  1. When evaluating each of the two expressions, what is done first? * : multiplication
  2. The first operation that gets done - where is that located in the 'Order Of Operations' table? At the top
  3. When evaluating both of the expressions, what is done second? + : addition
  4. Where is that operation located in the 'Order Of Operations' table? Below the first operation
  5. When evaluating the expressions, what is done last? = : assignment
  6. Where is that located in the 'Order Of Operations' table? Below the other operations
  7. Describe, in your own words, how we decide what order to apply the operations highest op first, then next highest, etc, all according to the table
  8. Evaluate the following expression, writing out the intermediate steps as shown above:
    y = 1 + 2 * 3;
    first 2 *3, then 1+6, then assign 7

 

Model 2: Order Of Operations

Arithmetic, Assignment, Comparison Operators

Example A

int x = 4;
int y = 7;
if( x + 2 > y - 2 )
if( x + 2 > y - 2 )
if( 4 + 2 > y - 2 )
if( 6 > y - 2 )
if( 6 > y - 2 )
if( 6 > y - 2 )
if( 6 > 5 )
if( 6 > 5 )
if( true )
Order Of Operations
* Multiplication
+ Addition
> or <= Comparison:
A > B is true if A is strictly greater then B (and false otherwise).
A <= B is true if A is less than or equal to B (and false otherwise)

Example B

int x = 4;
int y = 7;
if( y + 2 <= x * 2 )
if( y + 2 <= x * 2 )
if( y + 2 <= 4 * 2 )
if( y + 2 <= 8 )
if( y + 2 <= 8 )
if( 7 + 2 <= 8 )
if( 9 <= 8 )
if( 9 <= 8 )
if( false )

Critical Thinking Questions

  1. When evaluating each of the two expressions, which operation is done first?
    Where is that located in the 'Order Of Operations' table? * : multiplication - it's at the top of the table
  2. When evaluating each of the two expressions, which operation is done second?
    Where is that located in the 'Order Of Operations' table? * : addition - it's at the middle row of the table
  3. When evaluating each of the two expressions, which operation is done third?
    Where is that located in the 'Order Of Operations' table? * : comparison- it's at the bottomof the table
  4. Describe, in your own words, how we decide what order to apply the operations highest op first, then next highest, etc, all according to the table
  5. Evaluate the following expression, writing out the intermediate steps as shown above:
    if( y * 3 < x - 3 )
    (pick your own values for x and y) first 7 *3, then x - 3, then compare the two

 

Credits / Copyright Info