Learning ActionScript 2.0 in Adobe Flash |
|
|
|
| Syntax and Language Fundamentals > About operators > Using relational and equality operators | |||
Relational and equality operators, also called comparison operators, compare values of expressions, and they return either true or false (a Boolean value). You frequently use comparison operators in conditional statements and loops to specify the condition for when the loop should stop.
You can use the equality (==) operator to figure out whether the values or references of two operands are equal, and this comparison returns a Boolean value. String, number, or Boolean operand values compare using a value. Object and array operands are compared by a reference.
In this example, you can see how to use the equality operator to test the array's length and display a message in the Output panel if there are no items in the array.
var myArr:Array = new Array();
if (myArr.length == 0) {
trace("the array is empty.");
}
When you select Control > Test Movie, the string the array is empty appears in the Output panel.
You can use the equality operator to compare values, but you cannot use the equality operator to set values. You might try to use the assignment operator (=) to check for equality.
To use relational and equality operators in your code:
var myNum:Number = 2;
if (myNum == 2) {
// do something
trace("It equals 2");
}
In this ActionScript, you use the equality operator (==) to check for equality. You check whether the variable myNum equals 2.
The string It equals 2 appears in the Output panel.
var myNum:Number = 2;
to:
var myNum:Number = 4;
The string It equals 2 doesn't appear in the Output panel.
if (myNum == 2) {
to
if (myNum = 2) {
The string It equals 2 appears in the Output panel again.
In step 6, you assign the value of 2 to myNum, instead of comparing myNum to 2. In this case, the if statement executes regardless of the previous value of myNum, which can cause unexpected results when you test the Flash document.
For more information on correctly using the assignment operator, see Using assignment operators.
The strict equality operator (===) is similar to the equality operator, except it doesn't perform type conversion. If two operands are different types, the equality operator returns false. The strict inequality operator (!==) returns the opposite of the strict equality operator.
The following ActionScript demonstrates the key difference between the equality operator (==) and the strict equality operator (===):
var num1:Number = 32;
var num2:String = new String("32");
trace(num1 == num2); // true
trace(num1 === num2); // false
First, you define numeric variables: num1, and num2. If you compare the variables using the equality operator, Flash tries to convert the values to the same data type and then compare the values to see whether they are equal. When you use the strict equality operator (===) Flash doesn't attempt to do any data type conversion before it compares the values. As a result, Flash sees the variables as two separate values.
In the following example, you'll use the greater than or equal to (>=) operator to compare values and execute code based on the value a user enters into a text field.
To use the greater than or equal to operator in your code:
this.createTextField("myTxt", 20, 0, 0, 100, 20);
myTxt.type = "input";
myTxt.border = true;
myTxt.restrict = "0-9";
this.createEmptyMovieClip("submit_mc", 30);
submit_mc.beginFill(0xFF0000);
submit_mc.moveTo(0, 0);
submit_mc.lineTo(100, 0);
submit_mc.lineTo(100, 20);
submit_mc.lineTo(0, 20);
submit_mc.lineTo(0, 0);
submit_mc.endFill();
submit_mc._x = 110;
submit_mc.onRelease = function(evt_obj:Object):Void {
var myNum:Number = Number(myTxt.text);
if (isNaN(myNum)) {
trace("Please enter a number");
return;
}
if (myNum >= 10) {
trace("Your number is greater than or equal to 10");
} else {
trace("Your number is less than 10");
}
};
You can also check whether certain conditions are true and execute an alternative block if the condition is not true.
if (myNum == 10) {
trace("Your number is 10");
} else {
trace("Your number is not 10");
}
Except for the strict equality (===) operator, the comparison operators compare strings only if both operands are strings. If only one of the operands is a string, both operands convert to numbers and perform a numeric comparison. For more information on strings and operators, see About using operators with strings. For information on how order and operator precedence affect your ActionScript, see About operator precedence and associativity.
|
|
|
|