Assignment Operators

The assignment operators have two forms. The first is simple assignment, which has as symbol the equal sign, '='. The simple assignment expression:

	x=y-x

means that the value of 'x', the left side, is assigned or replaced or loaded with the value of 'y-x', the value of the expression on the right side. The equal sign is the assignment operator. This is not an equation as in algebra, nor is it a comparison for equality (that operator is: ==).

The second form of the assignment operator combines an operation with assignment. Lexically, this takes the form of the operator followed (without white space or a comment) by the assignment operator, '='.

There are 10 possible forms:

	*=  /=  %=  +=  -=  <<=  >>=  &=  ^=  |=

which involve the: multiplication, division, remainder, addition, subtraction, left shift, right shift, bitwise AND, bitwise XOR, and bitwise OR operators.

The meaning of: 'expression1' 'operator' 'assignment' 'expression2' is the same as: 'expression1' = 'expression1' 'operator' 'expression2'. Thus:

is a short hand notation for:

	x[y*10-t]=x[y*10-t]+9

Because assignment is an operator like all of the other operators, it can appear more than once in an expression. That is a single complex expression, besides calling functions and performing calculations can cause zero, one, or more assignments to take place. The key to this is that the value of an assignment operation is the value assigned. Thus the following expression:

	z=(y=x+=9-t)*(w/=v);

will make 4 assignments. It will assign 'w' with the value of 'w' divided by 'v'; it will assign 'x' with the value of 'x' plus 9 minus 't', which will also be assigned to 'y'. Lastly, the value assigned to 'y' will be multiplied by the value assigned to 'w' and this product will be assigned to 'z'. Whether 'z' or 'w' will be assigned before or after 'y' and 'x' is not defined by the language, and it is up to the compiler to choose whichever order is most convenient for it.


© 1991-2008 Prem Sobel. All Rights Reserved.