home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book HomeActionScript: The Definitive GuideSearch this book

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


In ActionScript, every statement should end with a semicolon. (It's good form to use semicolons, but not officially mandatory. In Chapter 14, "Lexical Structure", 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.

6.2.1. 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:

{ statement1; statement2; statement3... }

By using a statement block as the substatement of our if statement, we've managed to specify many statements where normally only one is expected. Very handy.

Anywhere ActionScript expects a single statement we may use a statement block. In fact, statement blocks are sometimes required. For example, the function statement must always include a statement block, even if the function being declared has only one statement in its body:

function aheadTwoFrames( ) {
  gotoAndStop(_currentframe + 2);

For the sake of readability, statement blocks are typically formatted as shown here:

parent_syntax {

where parent_syntax represents the statement for which we are defining a statement block.

Note that the opening curly brace appears at the end of the first line after parent_syntax. The substatements of the statement block appear indented two spaces, each on its own line. The closing curly brace, also on its own line, is flush with the original main statement. Substatements within statement blocks should end with a semicolon, but there are no semicolons on the lines with curly braces.

The indenting format is simply a convention, not a requirement of legal syntax. This book adheres to the style used by the Flash ActionScript editor.

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.