|The Trick Of The Mind - The Loop Zoo|
|Written by Mike James|
|Monday, 03 April 2023|
Page 1 of 3
Loops! They are fundamental to creating algorithms - but what are they and how many varieties are there? This is an extract from my book Trick of the Mind which explores what it is to be a programmer.
The Trick Of The Mind - Programming & ComputationalThought
Buy Now From Amazon
The Loop Zoo
This chapter is more directly focused on programming and programming languages than previous chapters. If you are not a programmer you might find that some of the sections go more deeply into how things work than you want or need to know.
Loops are an essential part of any program and becoming a programmer is mostly a matter of mastering the idea of controlled repetition. It is sad that most programmers only know the forms of loops provided by one or at most two languages because they have a life and structure that doesn't depend on language.
There is also the small problem that in more general thought loops tend to be hidden. We tend to lump the entire action of a loop into something complete and not at all loop-like. We tend to encapsulate loops in descriptions such as “knead the dough until it is ready”, “add sugar to taste”, “sand the surface until it is smooth”, “turn the volume up until it is loud enough”. One clue that a loop is involved is the use of words like “until” and “while”, but often it is left to you to work out that you are supposed to repeat an action until you have satisfied a condition. This makes loops less well understood by the average human than conditionals where the use of “if” acts as an explicit and easily recognizable signal.
As we have already discovered, there are only three types of flow of control in any program – the default sequential, the conditional and the loop. The sequential moves the program on from one instruction to the next in a given order; the conditional makes a choice about which instruction comes next based on some condition; and the loop simply repeats one or more instruction some number of times.
The loop is simple, but it is essential for reducing the amount of code we have to write. There is a well-known historical anecdote about Bob Belleville, who became head of engineering at Apple in the 1980s. While being interviewed for a job
Bob claimed "I've got lots of software experience, in fact I've personally written over 350,000 lines of code."
When this was relayed to Apple’s Rich Williams his comment was:
"Well, I bet he did, but then he discovered loops!"
Loops and nested loops mean that a small amount of code gets run and run. Surely, there is nothing more to say? In fact, there is much more to a loop which is perhaps the most interesting of the control instructions and loops come in many varieties.
Loops That Never End
As we learned in earlier chapters, back in dark days of programming loops were formed using the jump or Goto instruction that transferred control to a labeled instruction somewhere in the program. Conditionals also use the jump or Goto to transfer control to different parts of the program. The big difference is that the loop transfers control backwards to an earlier part of the program whereas a conditional usually jumps forward.
The effect of jumping back in the program text is that instructions that have been obeyed are executed again and hence a loop is formed. For example:
label: instruction1 instruction2 instruction3 Goto label
is the jump or Goto way of forming the granddaddy of all loops, the infinite loop. In the early days of assembler and unstructured languages this was the only way to form a loop. Of course, the Goto is dangerous because it can be used to goto anywhere in the program and so create a difficult to follow mess, see Chapter 4.
To stop programmers from writing unstructured code, languages introduced non-jump ways of writing a loop. This got rid of the Goto and the temptation to use it poorly, but it also obscured the mechanism of the loop. Many languages don’t have special instructions for an infinite loop of this kind, under the assumption that you probably won’t want to write one.
Some languages do provide an infinite loop, Visual Basic and VBA for example. While this particular form of writing loops hasn’t really caught on, it is helpful for understanding what is going on. Consider, for example:
You can see the general idea – the list of instructions between the Do and Loop statements are repeated, in this case forever. As before, you can see how the Do-Loop syntax hides the jump back to the start, but it is still there:
There are lots of variations on how you can write an infinite loop, but the most usual is to use a standard conditional loop with a condition that is always true. In Python, for example,:
While true instruction1 instruction2 instruction3
is a common way to write an infinite loop and it makes use of the fact that true is a literal that is always true. You will also see things like:
While 1==1 instruction1 instruction2 instruction3
which uses the fact that one is indeed always equal to one and hence the condition is always true but it looks mystifying if you haven’t seen it before.
It is important not to get too focused on the syntax of the way loops are written. Focus instead on the underlying idea.
|Last Updated ( Tuesday, 04 April 2023 )|