Compact Conditional Syntax

Throughout this book we use statement blocks (statements surrounded by curly braces) with all conditional statements, even if a block contains only one statement:

if (x == y) {
  trace("x and y are equal");
}

For the sake of terseness, ActionScript does not require curly braces when a conditional has only one substatement. A single substatement may quite legitimately be placed directly after an if or an else if statement without any curly braces, like this:

if (x == y) trace ("x and y are equal");

Or like this:

if (x == y)
  trace ("x and y are equal");

For some programmers, this style can be a little slower to read and slightly more error prone, though it undeniably saves room in source code.

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.