home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Book HomeActionScript: The Definitive GuideSearch this book

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;

5.1. 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.

5.1.3. Operator Precedence

An operator precedence determines which operation is performed first in an expression with multiple operators. For example, when multiplication and addition occur in the same expression, multiplication is performed first:

4 + 5 * 6  // Yields 34, because 4 + 30 = 34

The expression 4 + 5 * 6 is evaluated as 4 + (5 * 6) because the * operator has higher precedence than the + operator. When in doubt, or to ensure a different order of operation, use parentheses, which have the highest precedence:

(4 + 5) * 6  // Yields 54, because 9 * 6 = 54

Even if not strictly necessary, parentheses can make a complicated expression more readable. The expression:

// x is greater than y or y equals z
x > y || y == z

may be difficult to comprehend without consulting a precedence table. It's a lot easier to read with parentheses added:

(x > y) || (y == z)  // Much better!

Table 5-1 shows the precedence of each operator. Operators with the highest precedence (at the top of the table) are executed first. Operators with the same precedence are performed left to right.

Table 5-1. ActionScript Operator Associativity and Precedence

Operator

Precedence

Associativity

Description

x++

16

left to right

postfix increment

x--

16

left to right

postfix decrement

.

15

left to right

object property access

[]

15

left to right

array element access

( )

15

left to right

parentheses

function( )

15

left to right

function call

++x

14

right to left

prefix increment

--x

14

right to left

prefix decrement

-

14

right to left

unary negation

~

14

right to left

bitwise NOT

!

14

right to left

logical NOT

new

14

right to left

create object/array

delete

14

right to left

remove object/property/array element

typeof

14

right to left

determine datatype

void

14

right to left

return undefined value

*

13

left to right

multiply

/

13

left to right

divide

%

13

left to right

modulo division

+

12

left to right

addition or string concatenation

-

12

left to right

subtraction

<<

11

left to right

bitwise left shift

>>

11

left to right

bitwise signed right shift

>>>

11

left to right

bitwise unsigned right shift

<

10

left to right

less than

<=

10

left to right

less than or equal to

>

10

left to right

greater than

>=

10

left to right

greater than or equal to

==

9

left to right

equality

!=

9

left to right

not equal to

&

8

left to right

bitwise AND

^

7

left to right

bitwise XOR

|

6

left to right

bitwise OR

&&

5

left to right

logical AND

||

4

left to right

logical OR

?:

3

right to left

conditional

=

2

right to left

assignment

+=

2

right to left

add and reassign

-=

2

right to left

subtract and reassign

*=

2

right to left

multiply and reassign

/=

2

right to left

divide and reassign

%=

2

right to left

modulo division and reassign

<<=

2

right to left

bit-shift left and reassign

>>=

2

right to left

bit-shift right and reassign

>>>=

2

right to left

bit-shift right (unsigned) and reassign

&=

2

right to left

bitwise & and reassign

^=

2

right to left

bitwise XOR and reassign

|=

2

right to left

bitwise OR and reassign

,

1

left to right

comma



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.











??????????????@Mail.ru