1 / 17

Statements & Loops

Introduction to. Flash ActionScript 3.0. Statements & Loops. Thomas Lövgren, Flash developer thomas.lovgren@humlab.umu.se. Conditional Statements. A way for the computer to make a choice based on some condition ”If it is dark, then light the lamp, if not, leave it off.”

Download Presentation

Statements & Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Flash ActionScript 3.0 Statements & Loops Thomas Lövgren, Flash developer thomas.lovgren@humlab.umu.se

  2. Conditional Statements • A way for the computer to make a choice based on some condition ”If it is dark, then light the lamp, if not, leave it off.” • The operator checks a specific condition for the statement(s) • The condition could be either true or false depending on the setup • The if - statement is the most important conditional statement in ActionScript if (statement_one operator statement_two){ //run code }

  3. If & else statement • Example of a if-statement with the operator ">" (If var1 is greater than var2) if(x > 100){ //statement trace("X is too big"); //output } • Instead of writing another if-statement you can use the else- statement to catch when the condition evaluates to false if(x > 100){ trace("X is too big"); }else{ trace("X is not too big"); }

  4. Operators • Relational and Equality operators, evaluates to true or false Operator Function/task == If var1 is equal to var2 >= If var1 is greater than or equal to var2 > If var1 is greater than var2 <= If var1 is smaller than or equal to var2 < If var1 is smaller than var2 != If var1 is NOT equal to var2 && If var1 and var2 exist or both var1 and var2 are both set to true || If either var1 and var2 exist and/or are set to true

  5. Statements examples (1/2) if(hungry && !thirsty){ trace("go get a combo from the closest burger joint"); } if((variable_1 == variable_2) && (variable_3 > variable_4)){ trace("variable 1 is equal to variable 2 and variable 3 is greater than variable 4"); } if(theTimeOfDay == theMorning){ trace("wake up"); } else if(theTimeOfDay == myBedTime){ trace("go to bed"); } else if(iShouldBeAtWork){ trace("working right now"); } else { trace("watch some tv"); }

  6. Statements examples (2/2) //declare variables and assign values varvideoLoaded:Boolean = false; vargetData:Boolean = false; //if statement if(videoLoaded){ //is videoLoaded true? trace("The video is loaded"); //will not execute } if(!getData){ //is getData false? trace("The getData variable is false"); //will execute }

  7. Switch Statement • Switch Staements are useful when you want to respond to a series of possible values that a variable might have (instead of writing long If-structures) varswitchExpression:int = 3; //declare variable switch(switchExpression){ //switch statement case 0: trace(0); break; case 1: trace(1); break; case 2: trace(2); break; default: trace("not case 0, 1, or 2"); //traces not case 0, 1, or 2 }

  8. Loops • ActionScript loops are used to execute a segment of code repeatedly for a number of times or while a certain condition is satisfied • This can save time and effort by not having to type the same code multiple times to repeat a process • Example of loops: • For - loop • While - loop • Do/While loop

  9. Loops • Flowchart example of a loop

  10. For - loop • The for-loop is probably the most commonly used because it is the most compact and the easiest to understand and use • Structure for (counter; condition; action){ statements; } • Example for (var i:int = 0; i < 10; i++){ //for-loop trace ("This code is repeated ten times"); //output }

  11. While - loop • The While - loop repeats a set of code as long as the condition specified is true • Structure while (condition) { statements } • Example var i:int = 1; while (i < 5){ //condition trace ("This code is repeated"); //output i++; //increase counter }

  12. Do - While loop • A Do While loop will take a comparison statement and as long as the statement returns true, the loop will run • Structure do { statements; } while (condition); • Example var i:int = 1; do { trace ("This code is repeated"); //output i++; //increase counter }while(i < 5); //condition

  13. Reverse Loop & statement • It’s also posible to count down by reversing the values, and then decrementing the counter //reverse loop for (var i:int = 10; i > 0; i--){ trace("Hello: " + i); //output } • Example of an If-statement inside a for-loop: for (var i:int = 0; i < 100; i++){ //for-loop if(i > 50){ //if-statement trace(i + ": is greater than 50"); //output } }

  14. Array & Loops • Loops are very often used to manipulate and access the content of arrays varmovie_array = new Array(); movie_array = ["Batman", "The Godfather", "Star Wars", "Lord of The Rings", "Titanic"]; //loop through the array for (var i:int = 0; i < movie_array.length; i++){ trace (movie_array[i]); //output } /* Batman The Godfather Star Wars Lord of The Rings */ Titanic

  15. String & Loops • We can use a loop to go through a text string, and for example replace a word or a specific character //declare string and assign value varbook_string:String = "IT by Stephen King"; for (var i:int = 0; i < book_string.length; i++){ trace("Character " + i + ": " + book_string.charAt(i)); } /* output Character 0: I Character 1: T Character 2: Character 3: b …… */

  16. Named Loops • By naming a loop when you call break you can target the named loop as the break. This example shows a Nested for-loop: //my top loop outsideLoop:for(var i:int = 0; i < 10; i++){ trace(i + ": called from outsideLoop"); //output //my inside loop insideLoop:for(var j:int = 0; j < 5; j++){ trace(j + ": called from insideLoop"); if(j < 2) break outsideLoop; //stop the outside loop } }

  17. New Loops in AS3 • Examples of the new loop construct in AS3: //set up the array varmy_array:Array = ["test", "test2", "test3"]; //use a regular for in loop to access the properties in my_array for (varelement:String in my_array){ trace(my_array[element]); //traces test, test2, test3 } //use the new for each in loop to access the values in my_array for each (varindexData:String in my_array){ trace(indexData); //traces test, test2, test3 }

More Related