The Trick Of The Mind - The Loop Zoo
Written by Mike James   
Monday, 03 April 2023
Article Index
The Trick Of The Mind - The Loop Zoo
Loops That End
Fractional Loops

Loops That End

Infinite loops are sometimes taught as something you should avoid, but in many situations the entire structure of a program is organized around an infinite loop that keeps the whole thing running. However all good things come to an end and so do most loops, even supposedly infinite loops. 

It is only when we start to consider how loops come to an end do the really interesting variations come to the surface. You could say that this is where the family tree of loops starts to develop. 

There is one big split in loop classification – conditional loops and enumeration loops. If you are going to repeat a block of code, there are two possibilities:

  • Repeat the block until some condition is satisfied, e.g. repeat until A<0. This is a conditional loop

  • Repeat the block a given number of times, e.g. repeat 5 times. This is an enumeration loop. 

You might want to object and say that the enumeration loop is just a special conditional loop and you would be 100% correct. In fact, the only sort of loop you need is a conditional loop and, as explained in Chapter 3, a language with default flow of control, conditionals and conditional loops is Turing-complete.

So “repeat 5 times” can be written as “repeat until count>5” where we assume that count is incremented each time through the loop, see Chapter 4.

The essential difference between a conditional loop and an enumeration loop is that in the latter the number of repeats is explicitly known before the loop begins. With a true conditional loop you don't explicitly know how many repeats you are going to get before it starts. 

As the conditional loop covers just about every type of loop, it makes sense to look at it first and then specialize to the enumeration loop.

The Conditional Loop

Conditional loop are just infinite loops with a condition that has to be satisfied for them to terminate. You could say that they are infinite loops with exit conditions or exit points. How do conditional loops differ from one another? The major way that conditional loops differ is in the location and number of their exit points.

An exit point from a loop is simply an instruction that can bring the loop to an end and continue with the rest of the program. In many languages the command Break is used to terminate a loop but Exit Do is also used.

In this a loop with two exit points:

Do
instruction1
If condition1 Then Exit Do
instruction2
  If condition2 Then Exit Do
instruction3
Loop

you can see that there are two clear exit points that are taken when condition1 or condition2 turns out to be true. Loops with multiple exit points are intrinsically more complicated than ones with a single exit.

While and Until Exits

You can also write exit points in two forms – While and Until. 

The natural way to write an exit point is to specify the condition needed to stop the loop, i.e. the loop continues until the condition is true:

Until condition1 

which is the same as:

If condition1 Then Exit Do

This says that the loop continues until condition1 is satisfied, i.e. the loop ends when condition1 is true

A while condition is the opposite of an until – it is has to be true for the loop to continue, i.e. the loop continues as long as the condition is true. A while exit point's condition is just the logical NOT of the corresponding until exit point's condition making:

While condition1 

the same as

If not condition1 Then Exit Do

There isn't any deep difference between specifying the condition for the loop to stop or the condition for the loop to continue, but sometimes one is more natural than the other, as we will see. 

Single Exit Loops

In the old Goto days loops could, and often did, have lots of exit points, but this makes a loop difficult to understand. If you are reading instructions that form a loop, it is much easier to see how the loop comes to an end if it has only one way to do it. When the structured programming revolution came along it was decided that loops should only have a single exit point and although this restriction has been mostly ignored in modern languages, structured loops have a single, conditional, exit point. In the good old Goto days, things were very free about where you could put the exit point, but the structured programmers decided that you only needed at most two possible exit point locations – right at the start of the loop or right at the end of the loop. To be strictly accurate, you really only need one type, but it was thought having both made programs easier to write. 

Loops with an exit point at the start are usually called While loops because it seems to more natural to state the condition for them to continue. For example:

Do While condition 
instruction1
instruction2
instruction3

Loop

loops while condition is true. 

Similarly, a loop with an exit point at the end is called an Until loop because it seems more natural to state the condition for it to terminate, for example:

Do
	instruction1
instruction2
instruction3

Loop Until condition

There are some obvious, but usually not-discussed, differences between While and Until loops. The first is that in a While loop the condition has to be determined before the loop starts but in an Until loop it can be determined for the first time by the code being repeated. The second is that a While loop can terminate before it even begins, but an Until loop has to execute the loop code at least once. This is the important difference between the two loops – a While loop might repeat zero times, but an Until loop always repeats once or more times. 

This is the reason that we use a while condition in a loop with its exit point at the start. You want the loop to execute while a condition holds and if the condition already doesn’t hold then don't bother to loop at all. Similarly the until suits the end of the loop because you need to execute the loop at least once to see if the condition has been satisfied. For a loop with an exit point at the end you want to loop to continue until it has achieved something. 

If you don't think that these are natural, try the alternatives of having an until condition at the start or a while condition at the end. Note there are languages that allow you to do this.

First:

Do Until condition 
instruction1
instruction2
instruction3

Loop

which stops looping if the condition is true. Notice that the instructions might not be executed at all if the condition starts out true, which goes against the usual meaning of “do something until”.

Contrast this with:

Do 
instruction1
instruction2
instruction3

Loop While condition

which continues to loop if the condition is true. Notice that the while condition at the end of the loop can result in the body of the loop being executed once with the condition false which isn't logically nice. 

So While loops repeat zero or more times and Until loops repeat one or more times. 



Last Updated ( Tuesday, 04 April 2023 )