Order Of Operations

The goal for this exercise is to understand what order operators are executed in, so that you can both understand existing, and create your own, complex mathematical expressions

What you need to do to prepare for this exercise:


Microsoft has published a complete language specification for C#, which includes a table listing the precedence of each operator at:
https://docs.microsoft.com/en-us/cpp/c-language/precedence-and-order-of-evaluation?view=vs-2019.

This should closely resemble what you've seen in your math classes (PEMDAS and all that), with some exceptions. Note also that while you're not expected to recognize all the operators (for example, the bitwise ones are probably new), you should recognize a lot of them from Java / JavaScript (the logical AND/OR operators). This topic is explained in more detail in your textbook (Section 3.5 == ยง3.5).

Given each of the following expressions, what order will the operators be evaluated in, and why? After each operator is evaluated, re-write the expression, with any appropriate values taking the place of the operator.

What you need to do for this exercise

Fill in the table in this file: ExpressionEvalExercise.docx, as demonstrated in the first cell. Make sure that you hand this file in!

Expression Your Answer:
2 + 3 * 2.0

EXAMPLE:

  1. Annotated with types: 2[int] + 3[int] * 2.0[double]
  2. Multiplication goes first
  3. Convert 3[int] to 3.0[double]
  4. 2[int] + 3.0[double] * 2.0[double]
  5. Do the multiplication: 2[int] + 6.0[double]
  6. Addition goes next
  7. Convert 2[int] to 2.0[double]
  8. 2.0[double] + 6.0[double]
  9. Do the addition
  10. 8.0 [double] is the final result