Chapter 5. OperatorsContents:
General Features of 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 OperatorsThough 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.1. Operators and ExpressionsOperators 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 hold interim results for both convenience and clarity. Remember to name your variables descriptively, such as: var radius = 10; var height = 25; var circleArea = (Math.PI * radius * radius); var cylinderVolume = circleArea * height; 5.1.2. Number of OperandsOperators are sometimes categorized according to how many operands they take. Some ActionScript operators take one operand, some take two, and one even takes three: -x // One operand x * y // Two operands (x == y) ? "true result" : "false result" // Three operands Single-operand operators are called unary operators; operators with two operands are called binary operators; operators with three operands are called ternary operators. For our purposes, we'll look at operators according to what they do, not the number of operands they take. 5.1.3. Operator PrecedenceAn 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
5.1.4. Operator AssociativityAs we've just learned, operator precedence indicates the pecking order of operators: those with a higher precedence are executed before those with a lower precedence. But what happens when multiple operators occur together and have the same level of precedence? In such a case, we apply the rules of operator associativity, which indicate the direction of an operation. Operators are either left-associative (performed left to right) or right-associative (performed right to left). For example, consider this expression: a = b * c / d The * and / operators are left-associative, so the * operation on the left (b * c) is performed first. The preceding example is equivalent to: a = (b * c) / d In contrast, the = (assignment) operator is right-associative, so the expression: a = b = c = d says "assign d to c, then assign c to b, then assign b to a," as in: a = (b = (c = d)) Operator associativity is fairly intuitive, but if you're getting an unexpected value from a complex expression, consult Table 5-1 or add extra parentheses. We'll note cases in which associativity is a common source of errors throughout the remainder of the chapter. 5.1.5. Datatypes and OperatorsSome operators accept multiple datatypes as operands. Depending on the datatype of an operand, the effect of an operator may change. The + operator, for example, performs addition when used with numeric operands but concatenation when used with string operands. If operands are of different datatypes or of the wrong type, ActionScript will perform type conversion according to the rules described in Chapter 3, "Data and Datatypes", which can have serious effects on your code. Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|