Flash uses a built-in programming language called ActionScript which allows you to control aspects of your Flash movie.  You can provide flexibility and control over your movie as ActionScript does all the heavy lifting behind the scenes.  The preloader is a good example of this because it completes a test condition -- if frameisloaded() etc, without the viewer knowing all the details.

ActionScript allows you to declare variables using the var keyword but not their type.  For example, you can declare an ActionScript variable you call total this way:

var total = 0;

Unlike Java which is a strongly typed language, ActionScript (like JavaScript) is weakly typed.  This means there aren't any rules enforced over the use of the variables.  They can be treated as numbers and strings almost interchangeably.

ActionScript code goes into any frame of your movie, keyframe or regular frame.  You will not see ActionScript code appear in the movie (unless you type it in on the stage-but then Flash will just ignore it if you do).  Any frame containing ActionScript will have an "a" in the frame in the timeline.  

To enter ActionScript double click on a frame in the timeline and the Frame Actions window should appear.

On the right side of the Frame Actions window is the area where you can enter ActionScript code.

In the sample ActionScript below note how the statements are terminated with a semicolon and scoping is indicated by the use of the { and }.  The ActionScript keywords are indicated in bold.  These are var (for declaring variables), true,false (for setting a variable to a boolean), while (for constructing a while loop), length (this is applied to a string variable to obtain its length), charAt(i) (this returns the character of the string at position i), break (this causes the loop to end at that point), trace (causes an Output window to appear). 

Sample ActionScript

var source = "The quick brown fox jumped over the lazy dog";
var target = "x";
var found = false;
var i = 0;
while (i < source.length)
{
   if (source.charAt(i) == target)
   {
     found = true;
     break;
   }
   ++i;
}

trace((found?"Found":"Didn\'t find") + " (" + i + ")" + target + " in " + source);

This code will loop through each letter in the string variable called source and see if any letters match the target letter.   If the code finds a match, the loop ends (the break statement) at that point.  

The trace function causes a window titled Output to appear.  Only when you are testing the movie will this window appear - it won't if you publish and then run the Flash movie.  The Output window will display 

"Found (18)x in The quick brown fox jumped over the lazy dog."

Why?  Because the code will find the target letter x in the string at position 19.  The index started at 0 so we have an offset of one.

The notation  (found?"Found":"Didn\'t find")  is an advanced shortcut of the if-then-else clause.  The variable found is either true or false.  If it is true, then the true part is applied and that is indicated by the ? symbol.  If found is false, then the false part is applied and that is indicated by the : symbol.  

Note how the ActionScript's trace statement used the "+" operator to construct a new string by sticking variables together (this process is called concatenation).  The variables don't have to be all strings as you can see because "i" is used as an integer.

Other example ActionScript string functions are demonstrated below.  Note the difference between the substring and the substr functions.

trace("length of a fox is " + ("fox".length));
trace("part of a dog is " + ("dog".substr(1)))
trace("Upper fox is " + ("fox".toUpperCase()))

trace("3rd char of Flash is " + ("Flash".charAt(2)));
trace("sh= " + ("Flash".substr(-2, 2)));
trace("lash= " + ("Flash".substr(1, 4)));
trace("las= " + ("Flash".substring(1, 4)));
trace("index of a = 2? " + ("Flash".indexOf("a")));
trace("index of sh = 3? " + ("Flash".indexOf("sh")));
trace("index of sa = -1? " + ("Flash".indexOf("sa")));