Flash / Flex / ActionScript/Statement/for
Содержание
- 1 break is a keyword that allows you to stop processing a loop or conditional.
- 2 Nested for loops
- 3 Perform more complex initializations and increments by adding additional expressions separated by commas
- 4 Place two statements in the for statement
- 5 The for Statement
- 6 Use a for loop to check whether a string contains the @ character
- 7 Use for each loop to go through the array elements
- 8 Use For in loop to display all properties in an object
- 9 Use for loop statement to display all elements in an array
- 10 Use for loop to loop through the dynamic array
- 11 Use for statements to loop backward:
break is a keyword that allows you to stop processing a loop or conditional.
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var needle:int = 3;
var haystack: Array = new Array(1,2,3,4,5,6);
for (var i:int = 0; i < haystack.length; i++) {
if (!(hay == haystack[i])) {
continue;
}
trace("I found the needle!");
break;
}
}
}
}
Nested for loops
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var j:Number;
for (var i:Number = 0; i < 3; i++){
trace(i);
for (j = 100; j > 97; j--){
trace("\t" + j);
}
}
}
}
}
0
100
99
98
1
100
99
98
2
100
99
98
Perform more complex initializations and increments by adding additional expressions separated by commas
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
for(var i:Number = 0, a:Number = 25; i < 25; i++, a -= 2) {
trace(i + " " + a);
}
}
}
}
0 25
1 23
2 21
3 19
4 17
5 15
6 13
7 11
8 9
9 7
10 5
11 3
12 1
13 -1
14 -3
15 -5
16 -7
17 -9
18 -11
19 -13
20 -15
21 -17
22 -19
23 -21
24 -23
Place two statements in the for statement
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
for (var i:int = 0, j:int = 10; i < 10; i++, j--) {
trace("i is " + i);
trace("j is " + j);
}
}
}
}
The for Statement
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var total = 2;
for (var i = 0; i < 2; i++) {
total = total * 2;
}
//here"s the equivalent while loop:
var total = 2;
var i = 0;
while (i < 2) {
total = total * 2;
i++;
}
}
}
}
Use a for loop to check whether a string contains the @ character
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var address = "me@moock.org";
var isValidAddress = false;
for (var i = 0; i < address.length; i++) {
if (address.charAt(i) == "@") {
isValidAddress = true;
break;
}
}
}
}
}
Use for each loop to go through the array elements
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var games:Array = ["P","S","L"];
for each (var game:* in games) {
trace(game);
}
}
}
}
Use For in loop to display all properties in an object
for (var element:String in targetObject) {
// do some action
}
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var weather:Object = new Object();
weather.temp = 45;
weather.conditions = "rain";
weather.pressure = 970;
for (var element:String in weather) {
trace(element + ": " + weather[element]);
}
}
}
}
Use for loop statement to display all elements in an array
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var myArray:Array = ["A", "B", "C", "D", "E", "F", "G"];
for (var i:int = 0; i < myArray.length; i++) {
trace("Hello, " + myArray[i] + "!");
}
}
}
}
Use for loop to loop through the dynamic array
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var info:Object = new Object( );
info.city = "Toronto";
info.country = "Canada";
for (var detailName:* in info) {
trace(detailName);
}
for (var detailName:* in info) {
trace(info[detailName]);
}
}
}
}
Use for statements to loop backward:
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
for (var i:int = 10; i > 0; i--) {
trace(i);
}
}
}
}