Control+flow

Control flow
Programming languages only provide a few types of decision structures. The structures allow you to:
 * //loop// or //iterate// by running a set of code multiple times
 * //branch//, that is, run one set of code in one case, or run a different set of code in another case
 * //handle exceptions//, that is, try one set of code, and, if it doesn't work, run an additional set of code

These decision structures decide how many times to loop or which branch to run based on some condition or test. You can create a condition using the //comparison operators// and the //logical operators.// The comparison operators test whether two things are equal, or whether one quantity is greater or lesser than another. The logical operators allow you to combine sets of comparisons.

**Comparison operators**
Comparison operators compare two variables and return true or false:

For example: code format="actionscript3" var result:Boolean; result = (5 > 1);           // result is true result = (1 > 5);           // result is false result = (1 == 1);          // result it true result = (5 >= 5)           // result is true result = (true == false);   // result is false result = ("matt" == "matt"); // result is true result = (true != false);   // result is true code

**Logical operators**
Logical operators are:
 * && (the //and// operator)
 * || (the //or// operator)
 * ! (the //not// operator)

For example: code format="actionscript" var result:Boolean; result = true && true;  // result is true result = true && false; // result is false result = true || true;  // result is true result = true || false; // result is true result = !true;         // result is false code

**Branching**
Here's an if-else branch: code format="actionscript3" var name:String = "Matt"; var greeting:String;

if(name == "Matt"){ greeting = "Hi Matt!"; }else if(name == "Nikki"){ greeting = "Howdy Nikki!"; }else{ greeting = "Hey Dude!"; } code

Here's a switch branch: code format="actionscript3" var name:String = "Matt" var greeting:String;

switch(name){ case "Matt": greeting = "Hi Matt"; break; case "Nikki": greeting = "Howdy Nikki"; break; default: greeting = "Hey dude!"; break; } code

**Iteration**
Here's a for loop: code format="actionscript3" var letters:Array = ["c", "a", "t"]; var word:String = "";

for(var i:int = 0; i < letters.length; i++){ word = word + letters[i]; } //total is now equal to 5 code Here's a "for each" loop: code format="actionscript3" var letters:Array = ["c", "a", "t"]; var word:String = "";

for each(var letter:String in letters){ word = word + letter; } code Here's a while loop: code format="actionscript3" var letters:Array = ["c","a","t"]; var word:String = "";

var i:int = 0; while(i < letters.length){ word = letters[i]; i++; }

// total is now equal to 9 code

**Exceptions**
code format="actionscript3" try { var sprite:Sprite = new Sprite; sprite.addChild(sprite); } catch(e:Exception){ trace(e); } code