The Trick Of The Mind - Advanced Loops
Written by Mike James   
Monday, 12 February 2024
Article Index
The Trick Of The Mind - Advanced Loops
The C Style Non-enumeration For Loop
Some Standard Loop Problems

The C Style Non-enumeration For Loop

This is a very technical section and can be, should be, skipped by the non-programming reader. For the reader who can program it might explain why for loops in many languages are not quite as simple as described in the previous section.

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 enumeration loop, i.e. a simple counting loop. That so many languages follow this primitive lead set by C is surprising. It can be used in a structured way to create an enumeration loop, but it is confusing and has no real advantages. Used without restraint and “creatively” it is as dangerous as the Goto.

In fact I can well imagine a “C For Considered Dangerous” paper stirring up just as much resentment as the original Dijkstra paper.

In C-like languages, Java, JavaScript, C++ and C# for example, the For loop is something like:

for(init; condition; update)

and the three statements in the loop can be more or less any valid statements in the language and any of them can be left out. Note that Python is the only popular language that doesn’t implement this form of For.

The init 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 is executed at the very end of each loop. 

In other words, the C-style for loop is equivalent to:

init
Do
 ....
 update
 if not condition Then Exit Loop
Loop

or

init
Do
  ....
  update
Loop while condition

You could say that the C for loop is just syntactic sugar over a While loop, but this isn’t the way it was invented.

You can also see that it isn't really a dedicated for loop at all. It is much more general than an enumeration loop, but you can easily use it to construct a for loop. This is a bit like discovering that the Goto may be capable of uncontrolled jumping around, but it can be tamed by restricting it to simple forms.

So, for example, you would write a simple for loop as:

For(i=1;i<=10;i=i+1)

In this case the init sets the initial value of the index, the condition brings the loop to a halt when it is false and the update increments the index. Used in this way we have an enumeration loop.

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

For i=1 to 10.

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 in existence at all. It allows “clever” programmers to write things like:

For(;i<10;)

which simply repeats the loop while i is less than 10. The initial value of i is set somewhere else and presumably the loop has an instruction to increase the value of i so that the loop eventually ends. There are much worse examples of “creative” use in real programs.

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 a conditional loop that lends itself to implementing an enumeration loop.

cover600

Advanced Enumeration – For In

Once you have seen the indexed enumeration loop you can generalize it to other forms by making it even more abstract. Instead of viewing the loop as repeating a given number of times, you can invent a loop that repeats as many times as needed to process each element in a list or, more generally, a collection of elements. For example Python, JavaScript and many other languages have the For... In loop to allow iteration over lists or more generally collections:

For w In words

This repeats the loop for each element in the words list. What happens is that the variable w is set to each of the words in the list one after another. As this is usually what you want to do it is more direct than asking for word number one e.g. words[1] and so on.

As always there is resistance to increasing abstraction from programmers who were taught how to use traditional for loops with an index. Once you have been trained to use indexed loops it can be hard to see how to do the job without needing to count the number of things you are processing.

It also has to be admitted that there are times when you need an index. For example, how can you return the number of an element in a list that satisfies a condition? To do this languages introduce a way of finding the index of any element – even so programmers still sometimes struggle with how to do things the “new” way.



Last Updated ( Tuesday, 13 February 2024 )