|Friday, 12 March 2010|
Page 2 of 3
The for loop
Now it is time to consider what is the simplest of all loops - the enumeration loop. It might be simple in theory but the way that languages such as PHP implement an enumeration loop is far from simple.
The most general PHP enumeration loop is based on the for instruction and this is clever, sophisticated and capable of being used in ways it was never intended to be. The for loop is great fun to play with but very confusing when you first meet it. To make things clear let's consider the simplest possible version of the for loop and postpone the full flexibility of the instruction to another article.
To write a for loop that repeats n times you would use:
Yes even this looks complicated! The first reason is that within the for loop there is an enumeration variable which counts the number of times the loop has repeated. In this example, the enumeration variable or index is $i and the first part of the for instruction:
sets this to 1 at the start of the loop. Each time through the loop the index has one added to it and this is the purpose of the final part of the for instruction:
it says "increment $i" each time through the loop. Finally the middle part of the for instruction specifies what value the index has to have for the loop to continue and in this case:
The index has to be smaller than or equal to n for the loop to continue. As the index increases by one each time through the loop this means that the index starts at 1, then goes to 2, 3 and so on all the way up to n. After this the index is bigger than n so the loop stops.
Consider the example:
This starts the loop off with $i set to 1. Each time through the body of the loop $i is increased by one so that the body of the loop is executed with $i set to 1, 2, 3 and so on.. The condition specified as the second part of the for instruction is evaluated at the end of each loop and the loop continues if it is true. In this case it is false when $i is 11 and the loop exits. This means that the body if the loop is executed 10 times with $i counting the repeats 1, 2, 3, 4, ... 9, 10. The loop sends 1<br/>2<br/> .. 10<br/> to the web page which displays as 1 to 10 on separate lines.
Notice that you can use the index variable within the loop but you cannot change its value if you expect the loop to work correctly. The loop index counts the number of times the loop has repeated and if you change it the loop will forget where it has got to. You can also use any variable you want to as a loop index but for reasons that are historical programmers tend to use $i, $j and $k.
In general if you want to repeat a loop from start to end values of the index you can use:
repeats the loop for $i equal to 5, 6, 7.. to 15.
The for loop really becomes useful, indispensable even, when it is used with data that naturally needs to be processed in order - arrays for example, or the rows of a database. In this case yet another special version of the for loop, the for each loop, is particularly useful.
Notice that in the same way that you don't really need the do-while loop you don't really need the for loop. You can create an enumeration loop using a while loop and an explicit loop counter. For example:
This while loop repeats the body of the loop for values of $i from 1 to and including 10. The value of $i is initially set to one and each time through the loop one is added to $i using the shorthand $i++ which means "add one to $i" - you can use $i=$i+1 if you want. Also each time through the loop the condition is evaluated and the loop exits when $i is 11.
is exactly equivalent to:
This said the for loop is in most cases easier to use and it is a clear expression of the idea that you are writing an enumeration loop.
|Last Updated ( Monday, 15 March 2010 )|