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( ...

Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.