Thursday, January 10, 2013

Operators

Operators are special functions that take one or more operands and return a value. An operand is a value—usually a literal, a variable, or an expression—that an operator uses as input. For example, in the following code, the addition (+) and multiplication (*) operators are used with three literal operands, 2, 3 and 4 to return a value. This value is then used by the assignment (=) operator to assign the returned value, 14, to the variable sumNumber.
var sumNumber:uint = 2 + 3 * 4; // uint = 14
Operators can be unary, binary or ternary. A unary operator takes one operand. For example, the increment (++) operator is a unary operator because it takes only one operand. A binary operator takes two operands. For example, the division (/) operator takes two operands. A ternary operator is an operator that takes three operands. For example, the conditional (?:)
operator takes three operands.

Some operators are overloaded, which means that they behave differently depending on the type or quantity of operands passed to them. The addition (+) operator is an example of an overloaded operator that behaves differently depending on the data type of the operands. If both operands are numbers, then the addition (+) operator returns the sum of the values. If both operands are strings, then the addition (+) operator returns the concatenation of the two operands. The following example code shows how the operator behaves differently depending on the operands.
trace (5 + 5); // Output: 10
trace ("5" + "5"); // Output: 55
Operators can also behave differently based on the number of operands supplied. The subtraction (-) operator is both a unary and binary operator. When supplied with only one operand, the subtraction (-) operator negates the operand and returns the result. When supplied with two operands, the subtraction (-) operator returns the difference between the operands. The following example shows the subtraction (-) operator used first as a unary operator, and then as a binary operator.
trace(-3); // Output: -3
trace(7-2); // Output: 5
Programming Actionscript 3. Powered by Blogger.