Statements do the work inside functions, methods, class bodies and frame scripts.
Directives such as import and use namespace tell the compiler
how to read the code around them. Declarations (var, const,
function, class, interface,
namespace, package) are on their own pages.
All statements and directives
| Statement | Does |
|---|---|
{ … } | A block: groups statements. It does not make a scope. |
expr; | Evaluates an expression: a call, an assignment, an increment. |
; | The empty statement. |
if … else | Runs code when a condition holds. |
switch, case, default | Jumps to the case equal to a value. |
while, do … while, for | Loops. |
for … in | Loops over property names or indexes. |
for each … in | Loops over values. |
break, continue, labels | Leave or restart a loop, or a labelled statement. |
return | Ends a function, optionally with a value. |
throw, try … catch … finally | Raise and handle errors. |
with | Puts an object first in the scope chain. |
super(…) | Calls the superclass constructor. |
default xml namespace | Sets the namespace for unqualified XML names. |
use namespace | Opens a namespace for the rest of the block. |
import | Makes a class or package available by its short name. |
include | Pastes another source file in at compile time. |
if … else
if (condition) statement [else statement]
The condition is converted to Boolean. There is no elseif
keyword; else if chains are an if inside the
else.
if (score > best) {
best = score;
} else if (score == best) {
tie = true;
} else {
tie = false;
}
if chain.switch
switch (expression) {
case value: statements
…
default: statements
}
The expression is compared with each case value in turn using strict
equality (===), and control jumps to the first that matches, or to
default. Case values can be any expression, including strings. Control
falls through into the next case unless break, return or
throw ends it.
switch (event.keyCode) {
case Keyboard.LEFT:
case Keyboard.A:
dx = -1;
break;
case Keyboard.RIGHT:
dx = 1;
break;
default:
dx = 0;
}
while, do … while and for
while (condition) statement
do statement while (condition);
for ([init]; [condition]; [update]) statement
while tests before each pass, do … while after, so its body
runs at least once. In for, the init part may declare
variables with var; they belong to the enclosing function, not to the
loop. Any part can be empty: for (;;) loops until a break.
for (var i:int = 0, n:int = items.length; i < n; i++) {
process(items[i]);
}
var line:String;
while ((line = reader.next()) != null) {
lines.push(line);
}
for … in
for ([var] name[:Type] in object) statement
Gives name each enumerable property name of the object in turn, as a
string: the indexes of an Array, the keys of an Object or
Dictionary (the key objects themselves for a Dictionary),
the indexes of an XMLList. Only dynamic properties are visited, never the
fixed properties a class declares, and the order is not defined.
for each … in
for each ([var] name[:Type] in object) statement
Gives name each value instead of each name: the elements of an
Array or Vector, the property values of an
Object or Dictionary, the items of an XMLList.
As with for … in, only dynamic properties of an object are visited.
for each (var book:XML in catalogue.book) {
trace(book.@isbn, book.title);
}
for each (var n:int in new <int>[2, 3, 5]) {
sum += n;
}
break, continue, labels and return
break [label];
continue [label];
label: statement
return [expression];
breakleaves the innermost loop orswitch; with a label, it leaves the labelled statement, which may be any statement or block.continuestarts the next pass of the innermost loop; with a label, of the labelled loop.- A label is a name and a colon before a statement. A
breakwithout a label cannot be used in a plain block that is not inside a loop orswitch. returnends the function. In a function typedvoidit takes no value; in a typed function, strict mode requires a value of that type on every path.
search: for (var row:int = 0; row < rows; row++) {
for (var col:int = 0; col < cols; col++) {
if (grid[row][col] == target) {
found = true;
break search;
}
}
}
with
with (object) statement
Names inside the statement are looked up on object first. An assignment
to a name the object does not have goes to the scope outside. It makes code harder to
check and to compile efficiently, so it is little used.
with (box.graphics) {
beginFill(0xFF0000);
drawRect(0, 0, 100, 50);
endFill();
}
with.default xml namespace
default xml namespace = namespaceValue;
Sets the namespace given to unqualified element names in XML literals and searched by
unqualified E4X names such as feed.entry. Without it the default is the
unnamed namespace (an empty URI). It lasts to the end of the function, like a
variable. An XML literal that declares its own default namespace is not affected.
var atom:Namespace = new Namespace("http://www.w3.org/2005/Atom");
default xml namespace = atom;
for each (var entry:XML in feed.entry) {
trace(entry.title);
}