Programmer's Wiki


408pages on
this wiki
Add New Page
Talk0 Share

Introduction Edit

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 Edit

For Edit

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 Edit

for i = 1 to 10 
   print i
next i

when to use it Edit

  • You need to run through the loop a certain number of times.

For Each Edit

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 Edit

for each obj in myCollection

when to use it Edit

  • You have a 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 Edit

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 Edit

while < #01 jan 2000#
  print "it's not the new millennium yet"

when to use it Edit

  • 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 Edit

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 Edit

   mailFound = checkForMail()
while not mailFound

A Do While loop is the same as a Do Until loop, except that the condition is reversed.

when to use it Edit

  • You always need to run through the loop once.

Choosing an Iterative Construct Edit

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 Edit

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 true, the loop will run forever. If you have a process which hangs, one of the first things to check for is an infinite loop.

Example searching a list for a match (non-termination bug):

i = 1
while i < list.length 
 if list(i) == searchValue then 
 end if

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 
   i = i + 1
 end if

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.