Anda di halaman 1dari 7

Program control flow in actionscript

if statement: if( expression ) { // code block } ifelse statement if (expression ) { // code block a } else { // code block b } ifelse 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 ifelse 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 refractor the ifelse if statement with switch statement. And sometimes you can ignore the break statement to let the flow fall out. the looping statements, such as while, dowhile 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. Thats all for the control flow. Keep things simple, makes your code maintainable and readable. While The while loop is like an if statement that repeats as long as the condition is true and the form of while statement is: while ( condition ) { // code block } Suppose we want to print the integers from 1 to 50, we can use the while statement like this. var i:int = 1; // declare the counter variable while ( i <= 50 ) trace(i); Run the code. And then you can get an error, after 15 seconds. As you see, in this form, it only requires the condition statement, so, its easy to forget updating the counter variable. If you forget updating the counter, the loop will become an infinite loop. So, we should update the counter variable. And the new version is here. var i:int = 1; // declare the counter variable while ( i <= 50 ) trace(i); i++; // update the counter Eh, if you try to run the code, you have to wait another 15 seconds So, whats the problem here?

The answer is the braces; we dont use the braces here. If you dont use the braces to enclose the code block, only the first statement following the while statement will be executed during the looping. So, i++; will be executed after the looping is finished. I suggest you always enclose the block of code in braces ({}). Although you can omit the braces if the block of code contains only one statement, it increases the likelihood that statements added later will be inadvertently excluded from the block of code. If you later add a statement that you want to include in the block of code, but forget to add the necessary braces, the statement will not be executed as part of the loop. This suggestion is also for conditionals. OK, the correct version is as below: var i:int = 1; // declare the counter variable while ( i <= 50 ) { trace(i); i++; // update the counter } If you want to update the counter in the condition test statement, then be careful about the boundary conditions. And the correct version will be: var i:int = 0; // declare the counter variable while ( i++ < 50 ) { trace(i); } This equals to: var i:int = 0; // declare the counter variable while ( i < 50 ) { i++; trace(i); } When you using the looping statement, remember to update the counter variable, and be careful about the boundary conditions. dowhile Similar but a little different from the while loop, the dowhile loop guarantees that the code block is executed at least once, because the condition is checked after the code block is executed. And the form of dowhile is as blow: Do { //code block

} while (condition); The difference between is that the while loop test the condition first, while the dowhile loop execute the code block first. So, the code block in dowhile is executed at least once. The following example shows their difference. while ( false ) { trace(The while loop); } do { trace(The dowhile loop); } while ( false ); Remember the code block in dowhile loop will be executed before testing the condition. for Maybe the for loop is the most common looping statement. Lets take a look at its form first. for ( initial block; test block; update block) { // code block } And the execution order of for loop is: 1. the initial block, and the initial statement will be executed only once while entering the for loop; 2. the test block; if true, then go to the code block, or finish the loop; 3. code block; 4. the update block, then go to the test block; The for statement put the three factors inside it. In general, we declare the counter variable at the initial block, test the condition in the test block, and update the counter at the update block. If we want to print 1-50 again with for loop, our code will be: for(var i:int = 1; i <= 50; i++) { trace(i); }

This is the regular form of for loop, actually, the compiler wont force you putting all the three factors inside the parenthesis (). You can declare the counter variable before the for loop, or put the test statement and update statement inside the code block. Lets rewrite the code. Like this: var i:int = 1; // declare the counter variable for(;;) { if( i > 50 ) // test statement break; trace(i); i++; // update the counter variable } Or even like this: for(var i:int = 1;i <= 50 ;trace(i),i++) { } These two ways are not recommended, because both of them may confuse you. The first one do too much in the code block, the second one put the code block inside the update block. Keep each block simple; just let them in charge of their own responsibility. Nested loop Sometimes, just one loop cant satisfy our requirement. We need to control two or more counter variables inside the loop. For example, when you deal with the matrix, you may need two variables to indicate the row and the column, or so on. In these situations, you need the nested loop. The nested loop means you nest the loop inside the loop. You can use the while statement or for statement. Now, lets write some code to get familiar with the nested loop. Remember the multiplication table? Maybe we all had recited it during our primary school. Lets try to print all the expressions of the multiplication table with ActionScript. And the form of the expression is as follows: 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6

9 * 9 = 81 And the range is from 1 to 9. The form of the expression can be rewrite as: i * j = the product of i and j Obviously, we need to variables here, for i and j. And, we can put I into the outer loop, j into the inner loop. So, the code will like: for(var i:int = 1; i < 10; i++) { for( var j:int = 1; j < 10; j++) { trace(i + * + j + = + i*j ); } } Look at the result carefully.

The expressions in red rectangle are the same; in fact, we just need to print one of them. And you just need to modify one condition, and then the redundant expressions will disappear.

Eh, for the first version of print the multiplication table, you can use just one loop, but its not so readable. for(var i:int = 1; i <= 81; i++) { trace( ((i-1)%9+1) + * +(Math.ceil(i/9))+ = + ((i-1)%9+1)*Math.ceil(i/9)); } Aha, dont try to write your code like this, it may make you boss angry if he read the code

Anda mungkin juga menyukai