
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
146
|
Chapter 6: Statements
var x; // Simple declaration
var x = 10; // Declaration with assignment
We’ll discuss the specific syntax of each statement throughout the rest of this chapter.
Statement Blocks
Some statements actually include other statements, or substatements, as part of their
syntax. For example, the if statement has this syntax:
if (expression) substatement;
The substatement, which is executed only if expression evaluates to true, can be a
single statement, such as a variable declaration statement:
if (x = = 5) var numFrames = 2;
or it can be a series of statements grouped together as a statement block:
if (x = = 5) {
var numFrames;
numFrames = 10;
play();
}
As you can see, a statement block is any number of statements on one or more lines,
surrounded by curly braces. Here we show three statements, separated by semi-
colons, all on one line:
{ statement1; statement2; statement3... }
By using a statement block as the substatement of our if statement, we can specify
multiple statements to be executed conditionally (only when the if statement is
true).
This can be very handy.
We can use a statement block anywhere ActionScript expects a single statement. In
fact, statement blocks are sometimes required. For example, the function statement
must always include a statement block, even ...