[AS3 Chapter 5] Loops
Areost :: Special & Secret :: Schooool
Page 1 of 1
[AS3 Chapter 5] Loops
Loop Basics
Loops are exactly what they sound like. They repeatedly go through a piece of code until a certain condition is met. While using loops, we must be careful because if we don't set a condition for the loop to stop at, we will create an infinite loop which will cause the program to crash. With the right knowledge, you won't have to worry about that though.
There are 5 basics types of loops which will be covered throughout this chapter and those are:
for
for... in
for each... in
while
do... while
The for Loop
The for loop repeats itself for a range of values of a variable. There are three pieces of information that you must include in order for the for statement to work. These are; a variable, a conditional statement for the loop to stop looping and something that either increments, decrements or changes the value of the variable so that it will eventually reach the condition.
In the following example, a variable is created for use with the loop. The initial value of the variable is set to 0. The loop will run as long as i is less than 3, and will increase the value of i by 1 every time the loop is run. The actual code in the loop traces the value of i throughout the entire time that the loop is executed.
The output will be:
0
1
2
For...In Loop
The For..In Loop is used to figure out the properties of an Object or Array type variable. (Oh yeah, that was the variable that I was missing, but I'm too lazy to add that now)
the output will be:
brown
black
male
The For Each..In Loop
The for each..in loop goes through the properties of an array or an object type variable and gives the name of the property. Using the same example from the For..In Loop:
fur
eyes
gender
While Loop
The while loop is exactly what it sounds like. It is a conditional statement that does something while a certain condition is true.
output will be:
0
1
2
3
4
Do... While Loop
The do while loop is basically a while loop, except the some is executed at least once.
output:
5
4
3
2
1
...and that's it for loops! Well, I've got the basics covered.
Loops are exactly what they sound like. They repeatedly go through a piece of code until a certain condition is met. While using loops, we must be careful because if we don't set a condition for the loop to stop at, we will create an infinite loop which will cause the program to crash. With the right knowledge, you won't have to worry about that though.
There are 5 basics types of loops which will be covered throughout this chapter and those are:
for
for... in
for each... in
while
do... while
The for Loop
The for loop repeats itself for a range of values of a variable. There are three pieces of information that you must include in order for the for statement to work. These are; a variable, a conditional statement for the loop to stop looping and something that either increments, decrements or changes the value of the variable so that it will eventually reach the condition.
In the following example, a variable is created for use with the loop. The initial value of the variable is set to 0. The loop will run as long as i is less than 3, and will increase the value of i by 1 every time the loop is run. The actual code in the loop traces the value of i throughout the entire time that the loop is executed.
- Code:
var i:int; //declare a variable for use with the loop
for (i = 0, i < 3, i++){
trace(i);
}
The output will be:
0
1
2
For...In Loop
The For..In Loop is used to figure out the properties of an Object or Array type variable. (Oh yeah, that was the variable that I was missing, but I'm too lazy to add that now)
- Code:
var myDog:Object = new Object; //create new object
myDog.fur = brown; // add properties to the object
myDog.eyes = black;
myDog.gender = male;
for (var i in myDog){
trace(i);
}
the output will be:
brown
black
male
The For Each..In Loop
The for each..in loop goes through the properties of an array or an object type variable and gives the name of the property. Using the same example from the For..In Loop:
- Code:
var myDog:Object = new Object; //create new object
myDog.fur = brown; // add properties to the object
myDog.eyes = black;
myDog.gender = male;
for each(var i in myDog){
trace(i);
}
fur
eyes
gender
While Loop
The while loop is exactly what it sounds like. It is a conditional statement that does something while a certain condition is true.
- Code:
var i:int =1;
while(i<5){
trace(i);
i++; // increment i
}
output will be:
0
1
2
3
4
Do... While Loop
The do while loop is basically a while loop, except the some is executed at least once.
- Code:
var i:int = 5;
do{
trace(i);
i--;
} while (i > 0);
output:
5
4
3
2
1
...and that's it for loops! Well, I've got the basics covered.
Similar topics
» [AS3 Chapter 1] Variables
» [AS3 Chapter 2] Functions
» [AS3 Chapter 6] Event Handlers
» [AS3 Chapter 3] Array Basics
» [AS3 Chapter 4] Conditional Statements and Operators
» [AS3 Chapter 2] Functions
» [AS3 Chapter 6] Event Handlers
» [AS3 Chapter 3] Array Basics
» [AS3 Chapter 4] Conditional Statements and Operators
Areost :: Special & Secret :: Schooool
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Nov 29, 2011 10:27 am by Karma
» Hi guys! Lalala
Mon Nov 28, 2011 9:26 pm by Pukito
» ARGHHHrrhhghghg
Sun Nov 27, 2011 1:25 pm by Pukito
» [Flash Game] - I don't even
Sat Nov 26, 2011 11:08 pm by Karma
» Hi, nice to meet you.
Sat Nov 26, 2011 12:48 am by Pukito
» I think I see ..
Sat Nov 26, 2011 12:47 am by Pukito
» Where Karma goes...
Fri Nov 25, 2011 2:14 am by Karma
» Guys
Tue Nov 22, 2011 9:11 pm by Pukito
» Karmini's Pose
Tue Nov 22, 2011 8:31 pm by Karma
» The Order (1)
Sat Nov 19, 2011 10:42 pm by Karma