AS3 Control Flow

The educational technology and digital learning wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Draft


[optional]. What appears enclosed in square brackets is optional.

Control Flow Statements at a glance

Condition

 if ( expr) { statement } [ else (expr) { statement } ]
 ??  if ( subscript  in  array) { statement } [ else { statement }]

Loops

 for (initialization; testExpression; update) { statement }
 for each (subscript  in  array ) { statement }
 for (subscript  in  array ) { statement }
 while (testExpression) { statement }
 do { statement }  while (testExpression)

Stop, continue, skip to next record, exit the loop

 break    The break statement causes an immediate exit from within a while or for loop.
 continue The continue statement causes the next iteration of a loop.
 next      The next statement skips to the next input line then 
          re-starts from the first pattern-action statement.
 exit [ expr]	The exit statement causes the program to branch to 
          the END statement (if one exists), else it exits the program.

In details

For now, adapted (borrowed?) from: Beginner guide to scripting

Conditions and Loops in details

Conditions and loops are the basic parts of any computer program. They are the "thinking" portion of a program. AS3 has the usual if, for, and while structures.

 * if/else
 * for
 * while

if/else

 if (condition) {
     //do something here
 }

when if is used, the actions enclosed between the braces will be executed only if a certain logical expression is true. If the condition is not met, the code inside the braces will be ignored.

 if (iNum == 0 ) {
     iNum = 34;
 }

If iNum has a value of 0, iNum will be reassigned to 34.

When an "else" is added, the code that follows the else will be executed if the condition is not met.

 if (iNum == 0 ) {
     print "fine";
 } else {
     iNum = 94;
 }
 

If iNum is not 0, then iNum will be assigned to 94. To spice things up, you can add a bunch of if's inside each else.

 if ( iNum == 0 ) {
     iVal = 2;
 } else if ( iNum == 1 ) {
     iVal = 3;
 } else if ( iNum < 0 ) {
     streq( sStringA, sStringB, MAX_DATA_LENGTH);
 } else {
     iVal = 10;
 }

The conditions are expressed as follows

 if (x == y)   # true if x has the same value as y 
 if (x != y)   # true if x doesn't have the same value as y 
 if (!x)       # true if variable x is empty (no value or a value of zero)
 if (x = y)    # %NOT% a test!!! gives to x the value of y... 
 if (x < y)    # <, >, <=, >=
 if (a == 1 && b == 2 )  # AND... true only if both statements are true
 if (a == 1 || b == 2 )  # OR... true if any statement evaluates to true
 if (x ~ y)    # true if x contains y. 
 if (x ~ /y/)  # true if x contains regular expression y.

for

The for loop is the most used loop. It is in almost every complicated method or function out there. Its structure is:

 for ( starting number; condition; implement number ) {
     // do something
 }
 

The program will go through the loop, starting at the starting number, and will do something with it, then restart the whole loop, after implementing the number. An example is necessary for this loop:

 for ( iNum = 0; iNum<15; iNum++ ) {
     iCount++;
 }

This is a simple for loop, just for explanation. The loop will start with iNum=0 (you must have previously defined iNum) and, since iNum=0 is smaller than 15 will add 1 to count (which must have also been defined previously). Since there is nothing left to do inside the loop, iNum will be incremented by 1, and the loop will continue. iNum=1 is smaller than 15, so count is incremented by 1 and since there is nothing else, iNum is incremented. The loop will break when iNum=15, because iNum=15 is not smaller than 15. The result of this loop is obvious, but normally, it is not easy to understand for loops.

while

The while loop is the less used loop, but it can be very useful. Its construction is:

 while ( condition ) {
     //do something
 }
 

The program will keep on doing that something until the condition IS met. For example:

 while ( iNum < 0 ) {
     say("Please enter a positive number");
 }
 

The program will keep on prompting for a positive number as long as the number imputed is not positive. If the user puts a positive number in on the first try, then the program will ignore the loop right away.

A good use of the while loop is to read some external data file. For instance, the following would read a bigram table, in order to computer the mean bigram frequency of each word in the input file.

 while ( erF = (getline < "bigram.table") ) {
    if ( erF == -1 ) { print "\n" "file " tfile " not found";}
    bigramFreq[$1] = $2 
 }


Other Scripting things:

There are a multitude of other functions that this small crash-course tutorial will not cover. The most basic will be briefly explained:

break

The break statement ends the loop in which it is nested (inside). No matter the circumstances, if the program comes upon a break statement, the loop will be instantly terminated.

return

The return statement ends the method or function in which it is nested. Like the break statement, it will automatically end all processes that are currently underway. If something follows, it will return the value of what follows.