Home arrow Flash arrow Page 2 - Manipulating Data with ActionScript in Flex Applications

Manipulating Data with ActionScript in Flex Applications

In this third part of a five-part series focused on using Flex and ActionScript together, you will learn about methods, arrays, and more. 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). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
May 15, 2008
  1. · Manipulating Data with ActionScript in Flex Applications
  2. · Expressions
  3. · Arrays
  4. · Objects
  5. · Inheritance

print this article

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 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++) {

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

blog comments powered by Disqus

- More Top Flash Game Tutorials
- Top Flash Game Tutorials
- Best Flash Photo Gallery Tutorials
- The Top Flash Tutorials for Menus
- 7 Great Flash Tutorials
- Adobe Creative Suite 5.5 Now Available
- Critical Flash Vulnerability Heats Up the Web
- More on Nonpersistent Client-Side Remote Sha...
- Nonpersistent Client-Side Remote Shared Obje...
- Using the Decorator Pattern for a Real Web S...
- Using Concrete Decorator Classes
- Delving More Deeply into the Decorator Patte...
- The Decorator Pattern in Action
- A Simple Decorator Pattern Example
- Decorator Pattern

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials