Programming – The Purpose Of Loops
What Are Loops?
Loops are a fundamental construct for many programs. In fact, all but the most basic of programs are likely to include at least one loop in them. Loops can be very useful and can save you, the developer, a lot of time.
I’ll use some examples to illustrate how your time is saved. All the examples provided in this article will be in pseudocode.
Printing A Number Sequence
Lets say that we want to print a sequence of numbers. Lets say, 1 to 100 inclusive. Now without loops we would have to do the following:
……. and so on.
Obviously not only is this time consuming, it is also very tedious. Now, lets have a look what happens to the code if we use a loop, in this case, a for loop.
Now, as you can see, the code above is a lot more slick, easier to write and less time consuming. The one possible disadvantage could be that its slightly harder to write and read (from a non-developers perspective). However, even programmers with the most basic of experience should understand what you are doing with the for loop.
The Purpose Of Loops
The purpose of loops is to repeat the same, or similar, code a number of times. This number of times could be specified to a certain number, or the number of times could be dictated by a certain condition being met. There are usually a number of different types of loops included in programming languages including for loops, while loops and do….while loops.
Two Ways To Implement Loops
So, the two ways you can use a loop are:
Repeating a block of statements with a specified and previously defined number of iterations to be completed. In our example above, this is the way we have used looping – to print the numbers 1 to 100 inclusive. We know to do this that we have to loop 100 times, there is no requirement for a condition to be attached to the loop.
Repeating a block of statements where the number of iterations is unknown and is based on other variables or conditions.
For example in a do….while loop you may have the following condition:
In this basic example we’ve told the program to keep the sprinklers set to true (or on) while the sun is shining. This loop will continue while the condition, sun=true, is true. When this condition becomes false, the loop will exit and the program will continue.
do….while loops must run at least once, regardless of whether the condition is true or not (as the condition isn’t checked before the code is ran the first time. If we only wanted the code to run when the condition is true (i.e. not even run once if it is false) we would use a while loop.
So, in our example, our do…while loop will run at least once and at maximum…… infinity (in theory, if it was possible for the sun to shine 24 hours a day forever!).
The code within the loop could run once, twice, 100 times, 1000 times etc etc.
The do…while loop that we have described here is dependant on the condition (in the brackets with ‘while’ at the end) holding true for it to remain looping. The number of loops is unknown. The only time the number of loops is known is when the loop actually exists. It is not known before or during the loop.