Chapter 11. Statements and Expressions
IN THIS CHAPTER
This chapter covers statements and expressions. Even though we have not formally gone over statements, you have already used them. A statement is simply a small piece of code made up of keywords, operators, and identifiers. Statements can be in one of six categories:
- Declaration statements
These statements involve declaring variables, creating functions, setting properties, declaring arrays, and so on. Here's an example:
var myVariable:String; // declares a variable
myObject._x = 235; //setting the horizontal position
var my_array:Array = new Array ();//creating an array
function myFunction (){ //creates a function - Expressions
These include any type of legal expression. Here's an example:
i++; //increase a variable
lName + space + fName; //combining variables - Flow modifiers
These include any statement that disrupts the natural flow of the interpreter reading the ActionScript. There are two subtypes of flow modifiers: conditional statements and loop statements.Conditional statements use Boolean answers to determine what to do next or what not to do next. Here's an example:
Loop statements run until a defined condition has been met. Here's an example where the trace function will be run while i is less than 30:
if (inputName == userName){
if (inputPassword == password){
gotoAndPlay("startPage");
}else {
displayMessage = "Double check your password";
}
}else if (inputName != userName){
if (inputPassword == password){
displayMessage = "Double check your user name";
}
}else{
displayMessage = "Double check your all your information";
}
for (var i:Number=0; i<30; i++) {
trace (i);
}
//output: (numbers 0-29) - Predefined functions
Functions that are predefined in ActionScript. Here's an example:
trace ("function"); //a simple trace function
gotoAndStop (2); //a playback function
getProperty( myMovie, _x ); //gets the horizontal position - Object statements
Statements that deal with and manipulate objects. Here's an example:
var myGrades:Object = { tests: 85, quizzes: 88, homework: 72 };
for (name in myGrades) {
trace ("myGrades." + name + " = " + myGrades[name]);
}
//output: myGrades.tests = 85
// myGrades.quizzes = 88
// myGrades.homework = 72 - Comments
This last category is one of a kind. It includes comments used in code merely as information for the user while in the Actions panel. The interpreter will skip over these comments. Here's an example:
//this is a comment used in ActionScript;
/*this
is
a
comment
block
*/
Breaking up statements into these simple categories is done only to help you understand the different types and uses of statements. We will go over a few of these categories in more detail later in this chapter.Now let's look at some of the basics of building these statements.