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 |
int x = 1;
int y = 2;
x = y = 3;
|
|||||
x = y = 3 |
|
||||
x = 3 |
|
||||
x = 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 |
Goals:
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 =