
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
116
|
Chapter 5: Operators
The left side of an assignment operation can never involve an operation. For exam-
ple, this code is common in mathematics, but it is illegal in programming:
x + y = 10; // ERROR!
As we’ll see later, equality comparisons between two expressions are performed with
the
==and != operators. For example, the following checks whether x+yis equal to
10:
var x = 1;
var y = 8;
trace(x + y = = 10); // Displays: false
Again, this is not quite the same as mathematics, where x+y=10asserts that x+y
is equal to 10, rather than checking whether that is the case.
Combining Operations with Assignment
Assignment operations are often used to set a variable’s new value, based in part on
its old value. For example:
counter = counter + 10; // Add 10 to the current value of counter
xPosition = xPosition + xVelocity; // Add xVelocity to xPosition
score = score / 2; // Divide score by two
Don’t confuse an equals sign, which is used to assign a value to a vari-
able, with the algebraic equals sign. For an explanation of the differ-
ence, see “Assigning Values to Variables” in Chapter 2.
ActionScript supports a shorthand version of assignment, called compound assign-
ment, that combines operators such as +, –, and / with the assignment operator to
form a single “calculate-while-assigning” operation. ...