Flash Gallery | Flash Templates | Flash Menu | Flash Design | Flash Audio & Video

flash page flip

Actionscript 3.0 video tutorials

+ Reply to Thread
Results 1 to 2 of 2

Thread: Tutorial 3 - the loops

  1. #1
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,459
    Rep Power
    8

    Tutorial 3 - the loops

    amazing Flash templates

    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

  2. #2
    Junior Member Settled In netzach is on a distinguished road
    Join Date
    Dec 2008
    Posts
    1
    Rep Power
    0

    Re: Tutorial 3 - the loops

    easy to understand your tutorial, thank you so much !

+ Reply to Thread

Similar Threads

  1. Tutorial 4 - le funzioni
    By Flep in forum Actioscript 3.0 base - tutorials
    Replies: 6
    Last Post: 13-09-09, 16:45
  2. Tutorial 3 - i cicli
    By Flep in forum Actioscript 3.0 base - tutorials
    Replies: 12
    Last Post: 23-06-08, 23:58
  3. Tutorial 7 - the packages
    By Flep in forum Object Oriented Programming - tutorials
    Replies: 3
    Last Post: 13-11-07, 02:42
  4. Tutorial 8 the constants
    By Flep in forum Object Oriented Programming - tutorials
    Replies: 0
    Last Post: 30-10-07, 06:47
  5. Tutorial Cercasi
    By joyz in forum Actionscript 3.0 base
    Replies: 22
    Last Post: 30-07-07, 10:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Optimization by vBSEO