====== Iteration ======
===== Introduction =====
Iteration is the process of performing some action on a collection of elements, one at a time. It is most commonly expressed using some form of loop syntax.
Iterative processes can also be expressed using [[recursion]].
This is often a more elegant way to express an algorithm.
However, it is typically more resource-intensive, and harder for first-time programmers to grasp.
This article is to help you choose an appropriate programming construct, but listing different types of loop, and giving a brief description of each. For more detail, see individual articles on the different loops.
===== Loop Constructs =====
==== For ====
The [[For|loop]] executes a given number of times, incrementing a counter each time it runs.
The counter is checked before each run of the loop to ensure that the boundary value is respected.
This is the most intuitive kind of loop, and is one of the first constructs used by many beginning programmers.
=== typical syntax ===
for i = 1 to 10
print i
next i
=== when to use it === * You need to run through the loop a certain number of times.
==== For Each ====
The [[For|Each loop]] will call a collection object in the correct manner to obtain child objects one at a time.
It will ensure that you get each object once and once only, although it is not usually possible to guarantee any kind of ordering. This is a very neat construct, which is present in most modern languages. It is a big code-saver!
=== typical syntax ===
for each obj in myCollection
print obj.name
next
=== when to use it ===
* You have an collection of objects.
* You want to run a series of commands once for each object in the collection.
* The collection supports the appropriate interface (for example, IEnumerable for COM objects).
==== While ====
The [[While|loop]] checks a condition each time before it runs the commands inside the loop.
Often, the actions performed within the loop are updating data which affect the condition.
While loops are also often used when waiting for something to happen outside of the currently executing process.
E.g. user input or another kind of interrupt, message or event.
=== typical syntax ===
while datetime.now < #01 jan 2000#
print "it's not the new millennium yet"
wend
=== when to use it ===
* You do not know how many times to execute the loop.
* You may not have to execute the commands inside the loop at all.
==== Do While ====
The [[Do|while loop]] is almost identical to the [[While|loop]], except that it checks its condition at the end of each loop.
This means that it always executes the commands in the loop at least once.
=== typical syntax ===
do
mailFound = checkForMail()
while not mailFound
A Do While loop is the same as a Do Until loop, except that the condition is reversed.
=== typical syntax (2) ===
do
mailFound = checkForMail()
until mailFound
=== when to use it === * You always need to run through the loop once.
===== Choosing an Iterative Construct =====
Which form of loop is most suitable to solve a given problem?
Sometimes, there is no "best" solution. However, the different constructs have nuances which lend themselves to different specific situations. This will depend on whether you need to execute the loop a fixed number of times (for loop), whether you're executing the loop once for each of a collection of objects, or whether or not you need to execute the loop at all, depending on a condition.
===== Warning: Common Bug =====
A very common bug in loops (especially while and do...while loops) is not putting commands inside the loop to update the data underlying the condition. If the condition is always false, the loop will run for ever.
If you have a process which hangs, one of the first things to check for is a non-terminating loop.
Example searching a list for a match (non-termination bug):
i = 1
while i < list.length
if list(i) == searchValue then
return(i)
end if
wend
This code will never terminate unless the searchValue is found in the first item of the list.
The corrected code is below.
Example searching a list for a match (non-termination bug fixed):
i = 1
while i < list.length
if list(i) == searchValue then
return(i)
else
i = i + 1
end if
wend