Statement Syntax
Statements
are
typically made up of
keywords
and
expressions
.
We’ve already seen the var statement,
which declares and optionally initializes a variable. Here’s an
example of the var statement’s general
syntax:
var numFrames;
The keyword (var in this
case) identifies the beginning of the statement. Then comes the
syntax required by the statement, which, in our example, is simply
the name of a variable, numFrames. Finally, a
semicolon marks the end of the statement.
Tip
In ActionScript, every statement should end with a semicolon. (It’s good form to use semicolons, but not officially mandatory. In Chapter 14, I’ll have more to say about semicolon usage.)
Some statements can take multiple forms. For example, we can use
var with or without an optional initial
assignment (in this case 10 is a simple expression whose value is
assigned to x):
var x; // Simple declaration var x = 10; // Declaration with assignment
We’ll learn 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( ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access