The Essence Of Loops |
Written by Mike James | |||||
Thursday, 10 June 2021 | |||||
Page 2 of 4
The Conditional LoopConditional loop are just infinite loops with a condition that has to be satisfied for them to terminate. How do they differ? 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. For example, a loop with two exit points:
While and Until ExitsYou can also write exit points in two forms - While and Until exits. The natural way to write an exit point is to write the condition needed to stop the loop i.e. the loop continues until the condition is true:
This says that the loop continues until condition1 is satisfied. A while condition is has to be true for the loop to continue i.e. the loop continues while the condition is true. Obviously a while exit point's condition is just the logical not of the corresponding until exit point's condition and this allows while exit points to be written as:
or more usually as
Obviously there isn't anything deep in either 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 point but this makes a loop difficult to understand. When the structured programming revolution came along it was decided that loops should only have a single exit point. So structured loops have a single conditional exit point. Again in the good old goto days things were very free about where you could put the exit point but again 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 for the loop. To be strictly accurate you really only need one type but it was thought having both made programs easier to use. Loop 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. That is
loops while the 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. That is:
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 - an While loop might repeat zero times but an Until loop always "repeats" once. 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 conditional already holds then don't bother to loop at all. 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 and a while condition at the end. Note there are languages that allow you to do this:
which stops looping if the condition is true and
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 condition false which isn't logically nice. So While loops repeat zero or more times and Until loops repeat one or more times. Fractional LoopsWhat about loops with exit points some where other than the start or the end of the loop? For example:
In this case instruction1 will be executed one or more times but instruction2 will be executed zero or more times. In fact the difference between instruction1 and instruction2 is even more pronounced. Instruction1 will always be repeated one more than instruction2. This is a bit like repeating the code so many and a half times. This is often thought to be unnecessarily complicated and having exit points in anywhere else than at the start or the end is frowned upon. In fact structured language enforced this by only providing the While and Until loop and no other way to break out of a loop early. Recently languages have included a break or exit statement that does allow the placing of an exit point any where you like. Use sparingly and with care. The ContinueNow we come to a fairly recent innovation in loops - the continue statement. The logic seems to be that if a language introduces a break statement that terminates a loop why not have a statement that continues a loop. That is:
In this case the loop ends when the while condition1 is false. What the Continue statement does is skip instruction2 if conditio2 is true. That is the Continue statement seems to transfer control to the end of the loop and starts another repeat. A few moments thought should make clear that this is just
The Continue statement doesn't have much to do with the structure of loops. It is really just a shorthand for an If statement that skips a block of code. You can argue that Continue and Break are covert ways of sneaking goto back into programming languages. More advanced versions of the break and the continue can be used to jump out of multiple nested loops. <ASIN:1871962587> <ASIN:1871962706> <ASIN:1871962439> <ASIN:1871962420>
|
|||||
Last Updated ( Thursday, 10 June 2021 ) |