AS3 - Operators PDF Print E-mail
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 operators

There 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:


	var a : Number = 5 + 10 - 15 * (3 / 4);

	

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:


	a++;

	a--;

	

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:


	var a : Number = 10;

	var b :Number = a++; // b will be 10, a 11

	


	var a : Number = 10;

	var b : Number = ++a; // b will be 11, a 11

	

Assignment Operators

Then 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:


	var a : Number = 10;

	a = a + 4;

	


	var a : Number = 10;

	a += 4;

	

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 Operators

You can compare variables values using comparison operators. 

Just like in math classes, we have greater than and lesser than (or equals to) operators:


	var a : Number = 4:

	var b : Number = 5;

	var comp : Boolean = (b > a); // true

	var comp2 : Boolean = (b >= a); // true

	var comp3 : Boolean = (b < a); // false;

	var comp4 : Boolean = (b <= a); // false;

	

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:


	var a : Number = 4:

	var b : Number = 5;

	var equal : Boolean = (a == b); //false

	var notEqual : Boolean = (a != b); //true

	

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:


	var number_s : String = "10";

	var number_n : Number = 10;

	trace(number_s == number_n); //true

	trace(number_s === number_n); //false

	

Now, let's see what happens when we compare a boolean with a number:


	var true_n : Number =  1;

	var true_b : Boolean = true;

	trace(true_n == true_b); //true

	trace(true_n === true_b); //false

	

note that, in both cases, a string containing "true" or "false" will never be equal to the corresponding Boolean


	var true_b : Boolean = true;

	var true_s : String = "true";

	trace(true_b == true_s); //false

	trace(true_b === true_s); //false

	

Logical Operators

Logical operators are often used in conditional statements as they can combine multiple conditions and produce a single output.

Available operators are NOT ( ! ):


	var closed : Boolean = false;

	var open : Boolean = !closed //open will be true

	

AND ( && ) :


	var leftHand : Boolean = true;

	var rightHand : Boolean = true;

	var bothHands : Boolean =  rightHand && leftHand; //true if both conditions are true

	

and OR ( || ):


	var leftHand : Boolean = true;

	var rightHand : Boolean = false;

	var atLeastOneHand : Boolean =  rightHand || leftHand; //true if at least one condition is true

	

You can also combine different operators, for example:


	var output : Boolean = (rightHand && leftHand) || (leftLeg && rightLeg); //true if at least one AND conditional is true

	

And even in this case you have the assignment version of both AND ( &&= ) and OR ( ||= ) operators.

 

Add comment


Security code
Refresh