DekGenius.com
Team LiB   Previous Section   Next Section

7.1 The Assignment Operator (=)

The assignment operator causes the operand on the left side of the operator to have its value changed to whatever is on the right side of the operator. The following expression assigns the value 15 to myVariable:

myVariable = 15;

The assignment operator also allows you to chain assignments, assigning the same value to multiple variables as follows:

myOtherVariable = myVariable = 15;

The previous statement assigns 15 to myVariable, and then also assigns the value (15) to myOtherVariable. This works because the statement:

myVariable = 15;

is an expression. It evaluates to the value assigned; that is, the expression:

myVariable = 15;

itself evaluates to 15, and it is this value (15) that is then assigned to myOtherVariable.

It is important not to confuse the assignment operator (=) with the equality, or equals, operator (==), which has two equal signs and is described in the Section 7.4, later in the chapter. The assignment operator does not test for equality; it assigns a value.

    Team LiB   Previous Section   Next Section