Chapter 5. Operators

An operator is a symbol or keyword that manipulates, combines, or transforms data. If you’re new to programming, you’ll notice that some mathematical operators, like + (addition) and - (subtraction) are very familiar. In other cases, you’ll have to learn special programming syntax even if the concepts are familiar. For example, to multiply two numbers, ActionScript uses the symbol * (the multiplication operator) instead of the X typically taught in grade school. For example, this multiplies 5 times 6:

5 * 6;

General Features of Operators

Though each operator has its own specialized task, all operators share a number of general characteristics. Before we consider the operators individually, let’s see how they behave generally.

Operators and Expressions

Operators perform some action using the data values (operands ) supplied. For example, in the operation 5 * 6, the numbers 5 and 6 are the operands of the multiplication operator (*). The operands can be any kind of expression, for example:

player1score + bonusScore;              // Operands are variables
(x + y) - (Math.PI * radius * radius);  // Operands are complex expressions

Observe in the second example that both the left and right operands of the - operator are expressions that themselves involve other operations. We can use complex expressions to create even larger expressions, such as:

((x + y) - (Math.PI * radius * radius)) / 2  // Divide the whole thing by 2

When expressions become very large, consider using variables to ...

Get ActionScript: The Definitive Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.