190
Syntax and Language Fundamentals
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:
1.
Create a new Flash document.
2.
Type the following ActionScript into Frame 1 of the Timeline:
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.
3.
Select Control > Test Movie.
The string
It equals 2
appears in the Output panel.
4.
Return to the authoring environment and change:
var myNum:Number = 2;
to:
var myNum:Number = 4;
Summary of Contents for FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Page 1: ...Learning ActionScript 2 0 in Flash...
Page 8: ...8 Contents...
Page 18: ...18 Introduction...
Page 30: ...30 What s New in Flash 8 ActionScript...
Page 66: ...66 Writing and Editing ActionScript 2 0...
Page 328: ...328 Interfaces...
Page 350: ...350 Handling Events...
Page 590: ...590 Creating Interaction with ActionScript...
Page 710: ...710 Understanding Security...
Page 730: ...730 Debugging Applications...
Page 780: ...780 Deprecated Flash 4 operators...
Page 830: ...830 Index...