| Main Menu |
|---|
| Games |
|---|
| Current Projects |
|---|
| Articles & Tutorials |
|---|
| Other works |
|---|
| AS3 - Operators |
|
|
|
| Thursday, 26 May 2011 10:36 |
|
Operators are special tools that you use to manipulate data in your code, like incrementing, decrementing, assigning and comparing variables values. Arithmetic operatorsThere are different types of operators in Actionscript 3. The first operators you'll surely meet are arithmetic operators. As the name says, they can be used to write numerical expression:
Available operators are addition (+), substraction (-), multiplication (*), division (/) and modulo (%). If you need to increment or decrement a value just by one, you can use the increment or decrement operators, like this:
The value of a will be increased by 1 in this case. You can prefix or postfix the operator, depending on what you need. Essentially, if you prefix it, the value of the variable will be incremented and then the value will be returned. Otherwise, the value will be increased after being returned:
Assignment OperatorsThen we have assignment operators. We've already use one of them in the previous examples to assign values to variables, the assignment operator (=). There are other assignment operators available that let you shortcut arithmetic expression then assignment. For example, these two example actually do the same thing:
The value of a will be increased by 4 using the addition assignment. We also have substraction assignment ( -= ), multiplication assignment ( *= ), division assignment ( /= ) and modulo assignment ( %= ). Comparison OperatorsYou can compare variables values using comparison operators. Just like in math classes, we have greater than and lesser than (or equals to) operators:
And check for equality and inequality. As we already seen, the single equal operator is used for assignment, so for comparison we use the double equal operator:
Unlike in other languages, like C++ or Java, in AS3 the == operator does type conversion and compares the contents of the object. So, if you compare a String with a number, if they contain the same numerical value("10" and 10, for example) they will be equal. If you want to prevent auto type conversion, the strict equality ( === ) operator comes to help. It returns true if both values and data type are equals. Of course you have a strict inequality operator as well ( !== ). To make it clearer, let's make a practical comparison between these two operators:
Now, let's see what happens when we compare a boolean with a number:
note that, in both cases, a string containing "true" or "false" will never be equal to the corresponding Boolean
Logical OperatorsLogical operators are often used in conditional statements as they can combine multiple conditions and produce a single output. Available operators are NOT ( ! ):
AND ( && ) :
and OR ( || ):
You can also combine different operators, for example:
And even in this case you have the assignment version of both AND ( &&= ) and OR ( ||= ) operators. |


