|The Essence Of Loops|
|Written by Mike James|
|Thursday, 10 June 2021|
Page 3 of 4
Enumeration loops simply repeat code a specified number of times. For example in many languages, Logo for example, you can write things like:
and this means repeat the code block ten times.
Of course this can be written as a conditional loop with the help of a counter.
This looks more complicated and it is a bit messy but enumeration loops often repeat code that need to make use of a counter. The counter starts at 1 and finishes at 10. In particular enumeration loops are often used to process arrays and other indexed data structures - and in this case a counter is used as the index. You could say arrays and for loops are natural partners.
This is the reason why enumeration loops are most often implemented as some sort of For loop which specifies the behaviour of the counter or index. For example:
will repeat the code block for values of i from 1 to 10 inclusive. In many ways this "explicit index bounds" form of the for loop is about as good as it gets. It is a suitable abstraction of the indexed ennumeration loop idea.
One subtlety of enumeration loops is are they While or Until style enumeration loops. That is can the body of the loop be skipped completely? In most cases the answer is yes but you need to find out in the language of your choice what happens in the case of For loops like:
does the loop execute zero times or once?
The sad news is that languages are not always as clear about ennumeration as they should be.
The C Style Non-enumeration For Loop
Languages that follow the lead set by C tend to use a complex form of for loop that is not sophisticated or abstract but a little too close to the metal to capture the idea of an indexed ennumeration loop.
and the three statments in the for loop can be more or less any valid statements in the language. The init statement is executed just once when the loop starts, the condition is evaluated at the start of each loop and if it is true the loop continues. Finally the update statement is executed at the very end of each loop.
In other words the C style for loop is equivalent to:
You could say that the C form of the for loop is just syntactic sugar over a while loop.
You can also see that it isn't really a dedicated for loop but you can easy use it to write a for loop.
So for example you would write the previous for loop as:
once you get used to it this form of the for is easy enough to use but it doesn't capture the idea of indexed enumeration as clearly as
It is often argued that the C form of the for is more versatile and more powerful but this is simply to miss the point that it really shouldn't be.
The C for loop was invented to stay close to the way the hardware works and to make enumeration loops more efficient because they could be translated more exactly to machine code. These are not good reasons for a modern abstract programming language that is trying to get away from the hardware to make use of it.
You may think that the C style for loop is wonderful because of its power - but this is praise for all the wrong reasons. An enumeration loop should be just that and not just prepackaged conditional loop.
Once you have seen the indexed enumeration loop you can generalize it to other forms.
repeats the loop for each element in the words collection.
The problem with all collection iteration loops is how do you synchronously iterate over two collections and how do you get an index if one is needed in the body of the loop?
Python solves the index problem with the enumerate object:
which puts the index in i and the element in w.
This also solves the synchronous iteration problem:
The first collection is iterated over in a natural fashion and the second uses the generated index.
|Last Updated ( Thursday, 10 June 2021 )|