Seamus
To review, our solution to the classic "Native American" problem was:
<cfset amount=18.00>
<cfloop index=theYear from=1624 to=2001 step=1>
<cfset amount = amount * 1.04>
<cfloop>
The heading lifting is being done in the <cfloop> tag:
setting the starting and ending values for year
setting the increment for year
testing for the end value of year.
We used this style of loop because we have a predetermined number of
steps, 377 (2001 - 1624). Even if we change the problem to specify
biannual interest, we still have a fixed number of steps... we just
change the step value to 2.
Then I redefined the problem to:
The Dutch purchased Manhattan Island from the "Native Americans"
for $18.00 in
er, ah... lets say 1624. If the money were invested at 4%, compounded
annually, when would the value exceed $200,000?
Now, we can't determine in advance how many steps are necessary,
because that depends on the calculation within the loop.
if we write this out abstractly, we have:
Start with an amount of $18,00
Start at a year of 1624
Repeat
Multiply the amount by 1.04
Add 1 to the year
until the Amount is Greater Than 2000
To translate this into CF we have:
<cfset amount=18.00>
<cfset year=1624>
<cfloop condition="#amount# LTE 200000">
<cfset amount = amount * 1.04>
<cfset year = year + 1>
<cfloop>
In the above, the <cfloop> tag does a lot less work than our prior
example... it only tests if the loop should continue (while the
condition is still true).
As before, the body of the loop computes a new amount... this is used
in the condition that controlls the loop
In addition, we increment the year within the body of the loop,
because it is the answer we seek (and it is no longer being computed
in the <cfloop> tag
If we want to compute biannual interest, we merely change the year
increment within the loop body:
<cfset year = year + 2>
So, we have covered the two basic types of loops:
a FOR loop (predetermined number of steps)
A WHILE loop (while a condition is true - unknown number of steps)
CF, in its benevolence, provides us with some specialized loops so we
can easily iterate over complex structures: lists, queries and
collections.
These are, in fact, just a more-convenient format of a FOR loop.
.... Next time.
HTH
Dick
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Structure your ColdFusion code with Fusebox. Get the official book at
http://www.fusionauthority.com/bkinfo.cfm
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists