Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Tutorial 3 - the loops

This is a discussion on Tutorial 3 - the loops within the Actionscript for beginners - tutorials forums, part of the Tutorials category; Tutorial 3 ? the loops Now that we saw what are Arrays and how to use them I carry on with ...


Go Back   Forum Flash CS3 Flash CS4 > English Forums > Tutorials > Actionscript for beginners - tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  2 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 21-11-07, 06:50
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,486
Rep Power: 6
Flep is on a distinguished road
Tutorial 3 - the loops

Tutorial 3 ? the loops


Now that we saw what are Arrays and how to use them I carry on with the next step of the basics of Actionscript 3.0.
In this article we will see what are the loops, the use of them, the different types of loop and how to use them.
Besides, the loops allow us to go through every element of an Array using the variable of control of the loop as index of the Array.

The loops are blocks of code that are repeatedly performed up to when a certain condition (called condition of loop) has a true value, meaning the condition is right.
Actionscript 3.0 has 5 types of loop available?

for ( the mostly used )

while

do - while

for - each - in

for - in

Let us see how to use them?

for loop


This loop is normally the mostly used and the easiest one to learn for those of you who are starting with Actionscript 3.0.
A ?fo?r loop is a structure which allows to repeat some actions from zero to a specified number declaring the number of iteration (the number of times the code will be performed). The loop could also be an infinite one.

To create a ?for? loop, there are 5 fundamental rules to follow:

1. Declaring the word ?for? so that Flash understand we are starting a loop
2. Declaring a variable of control
3. Declaring a condition of loop
4. Declaring an expression to increase the initial value
5. declaring a block of instruction to carry out

So, with Actionscript 3.0, we declare a ?for? loop the following way:
Code:
for(var i:int=0;i<10;i++)
{
	trace(i);
}
We obtain the following output

0
1
2
3
4
5
6
7
8
9


for , I declare the loop
var i:int=0; I declare a variable of control (with an initial value of 0 for this example)
i<10; the condition for the loop. The loop will loop the time that condition is true
i++; the expression to increase the variable of control. At each loop, the variable is increased of one
trace(i); the block of instruction which will be carry out at each iteration of the loop

Briefly this is what happens:
? Flash recognised that it is a loop thanks to the operator ?for?

? It carries out the block of instruction placed in between the brackets

? As it is a loop, Flash won?t carry out any code placed after the ?for? loop but will loop once again and increase the variable of control

? It checks if the condition is true or not. If it true, it will loop through the block of instruction once again

? When the condition is false, Flash will carry on with the rest of the code placed after the ?for? loop as you can see in the following example:
Code:
for(var i:int=0;i<10;i++)
{
	trace(i);
}
trace('fatto');
output:

0
1
2
3
4
5
6
7
8
9
fatto


As I have already explained the Arrays of Actionscript 3.0, we can now see how to ?navigate? them and access their elements using a ?for? loop.
Code:
var nomi:Array=new Array('filippo','andrea','giovanni','pietro','giacomo','luca','paolo');
for(var n:int=0;n < nomi.length;n++)
{
	trace(nomi[n]);
}
I create an Array
var nomi:Array=new Array('filippo','andrea','giovanni','pietro','giac omo','luca','paolo');
I create a for loop with as a condition the maximum value of the array?s length (using the property length of the Array Class seen in the second tutorial about the Arrays)
for(var n:int=0;n < nomi.length;n++)
I then use the variable of control (n in this example) to access every single elements of the Array via its index
trace(nomi[n]);

I obtain the following output

filippo
andrea
giovanni
pietro
giacomo
luca
paolo

while loop


The structure of a ?while? loop is very similar to a conditional logic (if) that we will see further on.
As before, this loop allows to repeat part of a code more times starting from zero declaring a number of iteration.
The difference with the ?for? loop is that the ?while? loop is declared the following way:

variable of control
while(condition of loop)
{
block of instruction
expression to increase
}

so
Code:
var j:int=0;
while(j<10)
{
	trace(j);
	j++;
}
trace('fatto');
gives the following output

0
1
2
3
4
5
6
7
8
9
fatto


as we can see, the result is the same as with the ?for? loop, the type of structure and the implemented logic of our application will tell us which type of loop is best to use.
Also, it is important to notice that the action trace(?fatto?); is not performed until the condition of the loop do not result false.

do while loop


This loop could be defined as a reversed version of the ?while? loop.
Its structure is as follow:

variable of control
do
{
block of instruction
expression to increase
}
while(condition of loop);

so
Code:
var k:int=0;
do
{
	trace(k);
	k++
}
while(k<10);
trace('fatto');
with the following output

0
1
2
3
4
5
6
7
8
9
fatto


Once again, in this case, the output does not change. The final result is the same as with the ?for? or ?while? loop. The condition of the structure of our application that we are developing will tell us which is the best type of loop to use (I will repeat myself saying that in 90% of the case, the ?for? loop is the one used).

for each in loop


This loop is fundamental to access the dynamic properties of an object or the elements of an Array via their index.
The structure is as follow:

for each(variableOfControl in Object)
{
block of instruction
}

So, let us say to have an object with more properties:
Code:
var oggetto:Object=new Object();
oggetto.nome='filippo';
oggetto.cognome='lughi';
oggetto.sesso='maschile';
oggetto.paese='cesenatico';
with a ?for each in? loop we can retrieve the properties of the object the following way:
Code:
for each(var p in oggetto)
{
	trace(p);
}
and obtain the following output

filippo
lughi
maschile
cesenatico


Adobe do not guarantee the order of the output other that in the case we are working with the XML and XMLList Class.
In fact, if we add a property to object of numerical type
Code:
var oggetto:Object=new Object();
oggetto.nome='filippo';
oggetto.cognome='lughi';
oggetto.eta=35;
oggetto.sesso='maschile';
oggetto.paese='cesenatico';
and obtain the following output

filippo
35
lughi
maschile
cesenatico


The following code shows a for each loop into an Array:
Code:
var nomi:Array=new Array('filippo','andrea','giovanni','pietro','giacomo','luca','paolo');
for each(var s in nomi2)
{
	trace(s);
}
and obtain the following output

filippo
andrea
giovanni
pietro
giacomo
luca
paolo

for in loop


This loop has the same structure as the ?for each in? loop. The unique difference is that the ?for each in? loop goes through the value of the variable when in the ?for in? loop, it retrieves the names of the variable.
Let us take a look at an example using the same Object as before:
Code:
var oggetto:Object=new Object();
oggetto.nome='filippo';
oggetto.cognome='lughi';
oggetto.sesso='maschile';
oggetto.paese='cesenatico';
and obtain the following output

nome
eta
sesso
paese
cognome


I would like to remind to those of you at the beginning with Actionscript 3.0 to exercise a lot and reach a good use of the loops.

Source files:
Attached Files
File Type: zip tutorial_3_cicli.zip (6.9 KB, 73 views)

__________________

 


I recommend: Essential Actionscript 3.0

- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
- I do not reply technicians pvt messages. Open a thread !

Last edited by Flep; 05-06-08 at 19:09..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Tutorial 4 - le funzioni Flep Actioscript 3.0 base - tutorials 2 11-11-08 11:18
Tutorial 7 - the packages Flep Object Oriented Programming - tutorials 3 13-11-07 02:42
Tutorial 1 the variables Flep Actionscript for beginners - tutorials 0 01-11-07 06:47
Tutorial 8 the constants Flep Object Oriented Programming - tutorials 0 30-10-07 06:47
Tutorial Cercasi joyz Actionscript 3.0 base 22 30-07-07 10:52


All times are GMT. The time now is 21:08.


Powered by vBulletin versione 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap


FlepStudio
by Filippo Lughi
P.IVA 03605860406