An Introduction to Programming: Programming Loops
Created | Updated Jan 28, 2002
Loops are one of the foundation stones of programming and they are pretty well
universal. This is a guide to the basics of writing a loop statement, and the
format these statements take. First I will look at example loops, then explain aspects of the format of loops.
Loops with the Condition at the Beginning
This type of loop is where the code between the start and end of the loop is only acted on if the condition is met.
To explain this better I will give a real life example of this form of loop based on playing a CD:
1) Track ends | |
2) There are more tracks | [The Condition] |
TRUE - goto 3 | FALSE - goto 5 |
3) Play next track | [Code within Loop] |
4) Track ends, Goto 2 | [Code within Loop] |
5) Play next disk |
Loops with the Condition at the End
This type of loop acts on the code between the start and end of the loop at least once and only stops performing it when the end condition is met. This means
that whether the loop condition is met or not the function is performed,
and so it is a part of the program that repeats itself where needed. For example I will use the real life situation of paying for a packet of sweets:
1) We are told the value of the sweets | |
2) We hand over a coin | [Code within Loop] |
3) Return to 2 if the value of the coins handed over is less than the value of the sweets. | [Loop Condition] |
4) Pick up receipt and sweets. |
Counting Loops
In other words a loop that is set to go around a set number of times, with these loops a value is defined, changed
and checked for it's meeting of the condition (In this case the loop ends when the condition is True and continues when the condition is False.).
For example I will use the real life situation of trying to find a house in a street:
1) Look at house number, check it against desired house number, if correct house goto 4 | [Loop condition] |
2) Goto next house | [Change of Loop Value] |
3) Goto 1 | |
4) Knock on door. |
Aspects of Loops
The Condition
This is fairly simple to define, it is a logical or Arithmetical1
equation which has a true or false outcome. For example: " A + B = 5" this is a condition which is true when the sum of A and B is equal to 5.
Now in the case of loops the condition is used to define whether you enter or leave a loop. Now it depends upon the nature of the loop your using whether
the true or false values are used to let you enter or leave. For the examples I will use True to allow Looping, False will stop looping.
A thing to remember about the condition is it has to be possible for it to change state, so the loop can stop. Basically this means within the
loop code the value of the variables used in the condition calculation have to change, so in the example above the value of A and B have to be changed
by the loop code or in the loop definition(for a clearer explainaition see The Problems of Infinite Loops).
The Code within Loop
This is basically the set of instructions that defines what happens each time you loop.
The Problems with Infinite Loops
Now this is the problem with writting loops, they have to end (except if you are a VERY experienced programmer).
If they don't end you may end up getting stuck in a bit of programming code, unable to continue.
There are a couple of simple points to remember, to avoid this problem:
- The value(s) the condition is based on must change.
- The condition must be possible, it must be an achievable goal.
Related Entries
Programming Entries
An Introduction to ProgrammingLanguage Specific Entries
Loops in C/C++/JavaLoops in Assembly Language