Now, let’s spend some time to review what we have talked about in the control flow.
At first, we talk about the schedule and introduce three concepts about the control flow, sequence, selection and repetition.
Sequence means the statements execute in order.
Selection means one or more statements’ execution depends on the state of your program.
Repetition means one or more statements will be repeated, until your program reaches a certain state.
Then we discuss the conditionals.
“if” statement:
if( expression ) {
// code block
}
“if…else” statement
if (expression ) {
// code block a
} else {
// code block b
}
“if…else if…” statement.
if (expression ) {
//code block a
} else if ( expression ) {
// code block b
…..
} else if (expression) {
//code block n-1
} else {
//code block n
}
Remember, the expression will be converted to Boolean value if the expression is not a Boolean expression. And only one code block will be executed, so, be careful about the condition when you using “if…else if…” statement.
And the switch statement:
Switch (expression) {
case value 1:
// code block 1
break;
case value 2:
// code block 2
break;
……
case value n:
// code block n
break;
default:
// default code
break;
}
You can refactor the “if…else if…” statement with “switch” statement. And sometimes you can ignore the break statement to let the flow fall out.
After the discussion of conditions, we turn to the looping statements, such as while, do…while and for. And the break, continue and label statement are also discussed.
When you use the looping statement, remember to declare the counter variable, test the condition and update the counter variable. Further more, be careful about the boundary condition; be clear about “greater than” or “greater than or equal” and so on.
For example, the two versions of print the numbers between 1 and 50:
The first one is less than or equal,
var i:int = 1; // declare the counter variable
while ( i <= 50 ) {
trace(i);
i++; // update the counter
}
The second one is less than,
var i:int = 0; // declare the counter variable
while ( i++ < 50 ) {
trace(i);
}
The common usage of “break statement”:
1. jump out from the switch block;
2. jump out from the looping block;
3. jump out from the looping block designated by the label, when the break statement with label statement.
The common usage of “continue statement”:
1. Skip the left code in the looping block, and begin the next iteration.
2. The program flow will jump to the code block designated by the label and execute the code from the top of the code block, if you use the “continue statement” with a label statement.
That’s all for the control flow.
Keep things simple, makes your code maintainable and readable.

April 3rd, 2009
Ntt.cc
Posted in
Tags: 
RSS Feed
Email Feed
[...] -Here is a great Actionscript 3.0 tutorial which discusses “Control Flow” for those of you getting started with Actionscript 3.0, or what to brush up on what you do/don’t know. It is pretty basic, but stuff everybody needs to know, so take a moment and check it out, if it is stuff you know click the little close button on your browser window/tab… no harm done. Check it out here! [...]