The Assignment Operator

Reading: Ch. 2.5

The major theme of this lecture was to develop a good understanding of the meaning and use of the assignment operator =. The text gives a very brief explanation of this in Ch. 2.5. However we feel that this is an important issue that deserves more discussion.

When using a statement such as

x = y+2;
it is important to understand its effect.

In generally, the computer does the following when faced with such an assignment.

  1. It evaluates the current value of the expresison on the right-hand side of the = operator.

  2. After computing the so-called rvalue it then stores that value into the memory location associated with the left-hand side of the statement (the so-called lvalue).

So in the above example, it finds the current value of variable y, calculates the value of y+2 and then stores that value into variable x. It is important to note that the value of y is not affected by this statement. Furthermore, it is important to realize that this statement is based only on the current variable values. If the value of y is later changed, that does not have any later effect on x (that is, x will not be bound to be equal to y+2 for all of eternity).


Last modified: Saturday, 22 January 2005