Loops
From Orielly
---------------------------
Wrapping repetitive tasks in an efficient
structure is the role of loops
---------------------
Loops are like a movie editor running a clip of film over and over again, or like a director wanting an actor to repeat his part twice before something else happens, or even while something else happens.
From Orielly
-------------------------
A programming loop is probably just what you think it is: Use it to go through the structure and then loop back to the start and do it again. There are a few kinds of loops, and the type you choose to use can help determine how many times your instructions are executed.
-------------------------
For Loop
From Orielly
------------------
This loop executes its contents a finite number of time
------------------
The director says, “I want you to start at the table and walk until you get to the door, and I want you to take one step at a time.”
So, the actor starts at the table and takes 20 steps (this is how many steps it takes to get to the door). If the man took 21 steps he would be past the door. A for loop is like saying, “I want you to walk or jump or fly for some amount of time or period, starting from some point and going to another.” An example of one is this:
for (var i:Number = 0; i < 3; i++)
{
trace("hello");
}
Our actor is “i”. “i” is a number. “i” starts at the number 0. “i” goes to 3, and “i” goes one number at a time.
From Orielly
----------------------------
Finally, the double-plus sign (++) is equivalent to i = i + 1, or add 1 to the current value of i.
----------------------------
The “++” operator means “take one step at a time”
The above script:
for (var i:Number = 0; i < 3; i++)
{
trace("hello");
}
tells “i” to take one step at a time starting at 0 and going no more than 3. And when it gets there, to trace “hello.”
So, remember this order:
starting position
Ending position
How many steps
And all of these are separated by semicolons.
Notice that “for” is set up like a function. It is a built-in function. Remember a function looks:
function_name(){};
Stuff goes inside of the braces.
for (var i:Number = 3; i > 0; i--)
{
trace("hello");
}
The “--” means “take one step at a time backwards”
So the starting position is 3 and it goes no further than 0 and it takes one step at a time, backwards.
while Loop
From Orielly
-----------------------------
The other kind of loop that you are likely to use is a while loop. Instead of executing its contents a finite number of times, it executes as long as something remains true.
-----------------------------
var num:Number = 0;
while (num < .5) {
num = Math.random();}
From Orielly (for above script)
------------------------------
Starting with a default value of 0, num will be less than .5 the first time into the
loop. A random number is then put into the num variable and, if it’s less than
.5, the loop will execute again. This will go on until a random number that is
greater than .5 is chosen, thus exiting the loop.
------------------------------
The while loop is like saying “as long as something”
The director says to the actor named 'num':
“start at the table (0) and keep changing roles, until a random role (number) less than .5 is chosen for you.”
So 'num' is 0 at first. As he walks he changes roles until a role that is a is randomly chosen for is selected by the director. However, this role cannot be any number less than .5. I guess the director doesn’t like such numbers.
To make it simpler. The director wants the actor to play different parts until he, the director, sees one he likes however, the number can’t be less than .5. If the number is less than .5 the actor has to keep changing roles until a number greater than .5 is chosen.
Looking at the script again:
var num:Number = 0;
while (num < .5) {
num = Math.random();}
As long as 'num' is less than .5 a random number is chosen for 'num' to play. If a number greater than five is chosen, then that is the number the director chooses.
It is read:
// 'num' you start at a number 0. You will play a random role(number) as long as you are a number less than .5.
We assume that if a number greater than .5 is chosen, he will not be a random number, but he will be the number that is chosen.