Manipulating Data with ActionScript in Flex Applications - Expressions
(Page 2 of 5 )
An expression is any ActionScript that can be evaluated. At its simplest, an expression might consist of just one literal value or one variable. More complex expressions combine several values and/or variables using operators. There are many types of operators in ActionScript, ranging from mathematical operators to Boolean operators to bitwise operators. Most operators operate on two operands. For example, the following uses variables as operands in conjunction with a multiplication operator:
unitValue * quantity
Generally, expressions are not used in isolation. For example, the preceding code multiplies the values from two variables, but it does not do anything with that product. That value would typically be used in an assignment statement or as part of a larger expression. Boolean expressions are often used inifandforstatements, which we’ll look at next.
Statements
Statements are the building blocks of an application. They define the actions and program flow. Statements tell the application to do something. They can consist of variable declarations, assignments, function calls, loops, and conditionals.
You’ve already seen examples of variable declaration statements, as in the following:
var total:Number;
An assignment statement uses the equals sign (=) to apply the value on the right side to the variable on the left. For example, the following code assigns the product ofunitValueandquantityto a variable calledtotal:
total = unitValue * quantity;
A statement can also be a call to a function. The following example calls atrace()function, which is a built-in Flash Player function that writes to the console when running a debug version of an application in the debug player:
trace("This is a simple statement.");
So far, you’ll notice that each of the statements ends with a semicolon. All statements of these types should end with semicolons in ActionScript. These types of statements comprise the majority of ActionScript statements. However, there are some statements that do not end in semicolons. Those statements are looping and conditional statements includingwhile,for, andifstatements.
Looping statements, such aswhile andfor, allow you to loop the execution of a group of statements as long as a condition is met. The following is an example of awhilestatement in ActionScript. This statement incrementstotalas long astotalis less thanmaxTotal:
while(total < maxTotal) {
total += 5;
}
You can useforstatements as a compact way to write common loops. Theforstatement syntax is similar to that of thewhilestatement, except that in place of the one conditional expression, aforstatement uses three expressions: initialization, condition, and update. The followingforstatement callstrace()five times:
for(var i:int = 0; i < 5; i++) {
trace(i);
}
Conditional statements use Boolean expressions to make the execution of some statement or statements conditional. The following example adds five tototaliftotalis less thanmaxTotal:
if(total < maxTotal) {
total += 5;
}
You can useifstatements on their own as in the preceding example. You can also useifstatements in conjunction withelseclauses. You can use these clauses only as part of anifstatement. If theifstatement conditional expression evaluates tofalse,elseclauses that follow are run until one of the conditions evaluates totrue. It is possible to nest conditionals, and by convention the nestedifstatement starts on the same line as theelseclause within which it is nested, creating what are often thought of aselse ifclauses (though they are technicallyelseclauses with nestedifstatements). The following example adds five tototaliftotalis less thanmaxTotal; otherwise, the code subtracts five:
if(total < maxTotal) {
total += 5;
}
else {
total -= 5;
}
The following interjects anelse ifclause that tests whether thetotalis 20 more thanmaxTotal. If so, it subtracts 10; otherwise, it goes to theelseclause:
if(total < maxTotal) {
total += 5;
}
else if(total > maxTotal + 20) {
total -= 10;
}
else {
total -= 5;
}
Next: Arrays >>
More Flash Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the book Programming Flex 2, written by Chafic Kazoun and Joey Lott (O'Reilly, 2007; ISBN: 059652689X). Check it out today at your favorite bookstore. Buy this book now.
|
|