|
course.wilkes.edu/CS125Labs |
||||
Experiment 12: Assignment Associativity
Because the assignment symbol ( Variable = Expressionis itself an expression that can appear to the right of an assignment operator. That is, a statement with the form w = x = y = z;is a valid statements, and uses a technique sometimes called assignment chaining. The question is, what happens when execution reaches this statement?
The answer depends on the associativity of the assignment operator. That is, if (((w = x) = y) = z)but if assignment is right associative, then the expression is evaluated as
(w = (x = (y = z)))
We can determine the answer with an experiment.
If we initialize w, x, y and z
to different values (say, 1, 2, 4 and 8), perform this assignment,
and then display their values,
the values that appear should provide us with enough information to
determine whether assignment is left or right associative.
That is, suppose that w = x = y = z;is executed, the actions will occur in this order:
On the other hand, if w = x = y = z;is executed, the actions will occur in this order:
Modify
You should now understand more fully how the assignment operator works, and the direction in which it associates.
|
Home Membership
|
|||
|
Last update: Tuesday, August 22, 2000 at 12:42:51 PM. |
||||