Areost
Would you like to react to this message? Create an account in a few clicks or log in to continue.
Latest topics
» GUYS. "DOOD" IS HERE!
[AS3 Chapter 5] Loops EmptyTue Nov 29, 2011 10:27 am by Karma

» Hi guys! Lalala
[AS3 Chapter 5] Loops EmptyMon Nov 28, 2011 9:26 pm by Pukito

» ARGHHHrrhhghghg
[AS3 Chapter 5] Loops EmptySun Nov 27, 2011 1:25 pm by Pukito

» [Flash Game] - I don't even
[AS3 Chapter 5] Loops EmptySat Nov 26, 2011 11:08 pm by Karma

» Hi, nice to meet you.
[AS3 Chapter 5] Loops EmptySat Nov 26, 2011 12:48 am by Pukito

» I think I see ..
[AS3 Chapter 5] Loops EmptySat Nov 26, 2011 12:47 am by Pukito

» Where Karma goes...
[AS3 Chapter 5] Loops EmptyFri Nov 25, 2011 2:14 am by Karma

» Guys
[AS3 Chapter 5] Loops EmptyTue Nov 22, 2011 9:11 pm by Pukito

» Karmini's Pose
[AS3 Chapter 5] Loops EmptyTue Nov 22, 2011 8:31 pm by Karma

» The Order (1)
[AS3 Chapter 5] Loops EmptySat Nov 19, 2011 10:42 pm by Karma

May 2024
SunMonTueWedThuFriSat
   1234
567891011
12131415161718
19202122232425
262728293031 

Calendar Calendar


[AS3 Chapter 5] Loops

Go down

[AS3 Chapter 5] Loops Empty [AS3 Chapter 5] Loops

Post by Pukito Sun Oct 09, 2011 9:04 pm

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.

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);
}
output will be
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. clown
Pukito
Pukito
Cobbler

Posts : 377
Monies : 4851
Reputation : 4
Join date : 2011-09-27
Location : MHMM.

http://www.areost.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum