The Essence Of Programming
Written by Mike James   
Monday, 29 October 2012
Article Index
The Essence Of Programming
Loops
Structured Programming

Loops

The conditional is fairly easy to understand, so is the loop but it has other surprises in store.

In many ways it is the loop that makes programs really magical and almost gives them a life of their own.

A loop is simply a repeated section of the list of instructions.

For example:

repaint the fuselage
until it is smooth enough

In natural languages we tend to hide the repetition almost as if we are ashamed of it – perhaps we are!

A more precise version of the repainting instruction would be something like:

repeat the paint instruction 
until the result is smooth enough

This form brings out more clearly that you really are being asked to repeat a section of the list of instructions – “paint your model” until some condition is satisfied.

In programming languages the repeat is made even clearer by writing it something like:

REPEAT
  Actions to be repeated
UNTIL condition

You can think of the REPEAT and the UNTIL as marking out the section of the list that will be repeated.

For some reason this “bracketing” is something that beginners find difficult. They seem to find it difficult to read a program and see exactly what it is that is being repeated. They seem to have a very hazy understanding of the way that the text is translated into what is to be done. If you are an experienced programmer you will find this unbelievable because it is so natural for you to read the loop and understand what it implies in terms of "doing". You could take your finger and follow the instructions being obeyed and when you reach the UNTIL you are very clear about what happens depending on the condition. Either you move on to do something else or "go back" and do the actions again. Beginners get very confused about what the condition is all about and exactly where the program goes back to.

Understanding the loop is one of the hurdles that the prospective programmer has to overcome.

Why is the loop so special?

Without the loop your program is only as long as the number of instructions it contains. With loops it can run and run!

For example, a program that will take forever to complete is:

REPEAT
  BEEP
UNTIL forever

which simply makes a beeping noise - forever.

Without loops programs would run for a fixed and very short time and wouldn’t be able to deal with repeat events such as you pressing a key on the keyboard.

The next question in how many types of loop do you need?

That is, if you can get away with a single conditional instruction – IF..THEN – how many different types of loop are there?

Given how complicated loops are it should come as a surprise to you to discover that you only need one type but it isn’t the REPEAT..UNTIL form!

The reason isn’t difficult to see if you ask yourself how many times REPEAT..UNTIL can repeat something. As the test to repeat the list of instructions only comes at the end you should be able to work out that the minimum number of times it is obeyed is once.

That is a REPEAT..UNTIL loop will repeat a list of instructions one or more times. Why isn’t this good enough to be a general repeat?

The answer is that you might want to have a list of instructions that you sometimes don’t repeat at all! For example,

paint the model
until you are happy with it

 

implies that you have to paint it at least once. It doesn't allow for the fact that you might be happy with it unpainted!

Most programming languages solve this problem by introducing the while loop -

WHILE condition
  Actions to be repeated
END WHILE

In this case the condition is tested before the actions are obeyed and if the condition is false the loop is completely skipped - that is the loop might stop before it has properly begun.

Beginners find the while loop hard. They find the way that the condition makes the loop continue rather than stop tough to deal with. Most of the time we think of doing something until something is achieved - e.g. stir the coffee until the sugar is dissolved. We are not good at applying conditions for the continuation of a loop - e.g. while you can see sugar stir the coffee. The first is natural the second seems fussy and awkward. (If you don't agree then it is likely that you have been a programmer for some time!)

The second problem with the While is that beginners find it very difficult to initially skip the instructions if the condition is already true. It is almost as if they see the text under the While Condition and are forced to read it and hence obey it by the usual laws of  sequence. This happens a little with the IF..THEN but it really seems to be a big problem with the WHILE.

The whole point is that the while is a loop that can be carried out zero or more times.

This makes the WHILE loop a little more powerful than the UNTIL loop but to be honest they are both useful and a little thought shows that the WHILE loop can be written as a combination of IF and UNTIL:

IF condition THEN
REPEAT actions UNTIL condition

Don’t worry too much if you don’t quite follow this.

The important point is that the choice of the particular type of loop that you decide to treat as the one essential loop is a bit arbitrary.

Again, as with the conditional, the important point is that you only need one type to write any program. That is, armed with the IF..THEN and the WHILE loop, any program that can be written using more elaborate commands, can still be written.



Last Updated ( Monday, 29 October 2012 )