In programming, we often get situations where we would like to repeat a certain piece of code again and again. Of course, we could just copy and paste this code one after another – however there are many negative elements to this.
- You’d have to know how many times you wanted to repeat the code at compile time – i.e. you could not calculate this at run time as you’d have to know beforehand how many times you need to ‘paste’ your code in. You may know the number of loops that you want to do at compile time, but then again – you may not.
- Your code becomes a monstrosity – picture the scene, block after block of code with the same things repeated over and over again. This becomes very hard to maintain, your code takes up more disk space (not as much of an issue these days as it once was when there was very limited hard drive space but still there should be a drive, as a programmer, to ensure that the code that you right is as clean and efficient as possible.
- It’s not maintainable – you’ll spend hours and hours maintaining one block of code that is actually now 1000 blocks of code that could have just used a loop. Imagine you need to change one calculation in your block of code. Now you have to do it 1000 times, 10,000 times, 100,000 times, maybe even 1 million times – you get the picture.
Loops can be used in programming languages where we want to do the same (or similar) set of actions (lines of code) multiple times. All modern programming languages support looping and most support the different common types of looping such as For Loops, While Loops and Do Loops.
Two Main Types of Loop
There are two main types of loops in programming – when we say types we don’t mean, for example, ‘for’ is a type of loop. We’re a level higher than that.
The first type of loop is where you are repeating a block of statements for a known number of times (iterations of a loop). For example, if you know before you enter a loop that you want to loop around it 365 times, one for each day of the year – then you are using this type of loop. This type of loop is known as a deterministic loop. This means that the number of iterations is known and defined prior to entering the loop. This loop will run the specified number of times unless it hits an ‘exit’ statement – which you can use in your code, for example to exit a loop if an error has occurred.
The second type of loop – and you may have guessed it already – is where you are repeating a block of statements for an unknown or indeterminate amount of time. For example, where the loop being completed depends on variables and results of a calculation being performed in the loop. While a condition is true, or untrue, the loop will continue until the condition is satisfied. As a programmer, you have to be careful with this type of loop – as there is the possibility for chaos. Remember the golden rule:
Indeterminate iteration loops where the number of loops is unknown until the loop is executed should always have an exit strategy!
What does this mean? It means, don’t get caught in an infinite loop (even the best get caught out sometimes)! Infinite loops not only hang your application – causing much annoyance to the user, but also can cause the system they are running on to run out of memory and crash – again this causes an even more infuriated user. Remember, defensive coding is king!
As an example, if you have an indeterminate iteration loop you obviously don’t know how many times it will loop – but we can take precautions to some degree in almost all circumstances where they are used. Firstly, errors and error handling. Can your loop continue if one of the passes encounter an error? Technically, a lot of the time it can – but from a programmatic logic point of view – should it continue and is there any point in it continuing. If a loop is going to occur hundreds of times but on the second loop pass there’s an error and this means your final result will be wrong, exit the loop and display an error message to the user rather than leave them hanging (in the application sense, literally!).
Secondly, for some indeterminate loops there’s a sensible ‘maximum’. For example, if your loop on average iterates 50 times, occasionally iterates 100 times and rarely iterates 250 times then it’d be sensible to code in your application that if we get to the 500th pass of this loop, somethings probably gone wrong – exit! You’d rather have a small bug to patch that is very infrequent for a minority of users than to have users banging their heads in frustration against the keyboard!
Another example would be reading a file. You might want to loop line by line through a file and output it to the screen. You could loop 50 times – but what happens if there are 66 lines in this document. This is another example where the indeterminate loop is your friend. The number of loop passes is indeterminate as the number of lines contained in a file that the user passes to your application is indeterminate. Whilst you may want to set maximums in your application – i.e. only files 1000 lines or less, you application won’t be very useful if you say “yes it can process files, as long as they are exactly 12 lines long!”.