If I were to say what's so cool about loops, I would say that they allow you to do one action many times in a short time (actually, in a split second).
Here goes the basic code:
- do {
- statement(s)
- } while ( condition )
Imagine that someone revealed the secret of eternal life and 2006 years ago placed a sum as small as $1 in a bank. The amount has been increasing by 1% every year. How big, do you think, the amount would be now?
The answer is: $466, 311, 159!
Here's how you can calculate it:
i. First off, create a new Flash document. The width and height don't matter, nor does the framerate.
ii. Copy and paste the following code on the first frame of the main timeline:
- money = 1;
- year = 0;
- do {
- year++;
- money = money + money *.01;
- } while (year<2006);
- trace(Math.round(money));
There you are. Now, if you press Ctrl + Enter, the output window should display
$466, 311, 159.
|
But how does this code exactly works? Let's have a closer look at it:
- money = 1;
- year =0
I declare veriables. "money" is how much we start with. Since we start with exactly one dollar, "money" should equal 1. The variable "year", on the other hand, is the current year. It is equal to 0, because this is the year that we start from.
money = money+ money*.01;
- year ++;
We increase the variable "year" by 1. We can as well write year = year + 1, but this one seems better, doesn't it?
- } while (year<2006);
Once the variable "year" is equivalent to 2006 the loop's going to stop. It means that the actions between {} will be executed 2006 times!
- trace(Math.round(money + "$"))
At the end, we tell Flash to trace the value of "money". The function Math.round() rounds a given numer to the nearest integer. For example, Math.round(3.22) will come out as 3. At the end, we add the mark "$" to the money.
swf_download_more_values();
$john2->title = $john2->title_arr['note'];
$john2->content = " Some of you might have noticed by now that the whole thing could be calculated much easier by means of compound interest. Here's how:
";
$john2 ->display_any_table();
?>
money = 1*Math.pow(1.01,2006)
trace(money)
Alright, you're now done with creating a simple loop.
I hope this tutorial has helped you to understand do while loops. You can find other tutorials at this address.
Cheers!

