What about loops with exit points somewhere other than the start or the end of the loop? What is so wrong with them? For example:
Do instruction1 If condition1 Then Exit Do instruction2 Loop
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 once 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 anywhere other 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 anywhere you like. They should be used sparingly and only when they make the intent clearer.
The Continue
Now 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:
Do While condition1 instruction1
If condition2 Then Continue Do
instruction2
Loop
In this case the loop ends when condition1 is false. What the Continue statement does is skip instruction2 if condition2 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:
Do While condition1 instruction1
If Not condition2 Then instruction2
Loop
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.
In book but not in this extract
Enumeration Loops
The C Style Non-enumeration For Loop
Advanced Enumeration – For In
Some Standard Loop Problems
Nesting And Recursion
Summary
The basic loop is an infinite loop.
Finite loops have exit conditions that terminate the otherwise infinite loop.
An exit condition that has to be true to exit the loop is an Until condition.
An exit condition that has to be false to exit the loop is a While condition.
Loops can have any number of exit points positioned anywhere in the loop, but structured loops have a single exit point, placed either at the start or the end of the loop.
Loops with a start exit point are generally called While loops and use a While condition. While loops can skip the body of the loop completely, i.e. they can execute the body zero or more times.
Loops with an end exit point are generally called Until loops and use an Until condition. Until loops always execute the body of the loop at least once, i.e. they can execute the body one or more times.
The break and continue instructions allow you to create loops that go beyond the basic While and Until loop.
An enumeration loop is one the repeats a given number of times known before the loop starts. An enumeration loop is just a special conditional loop but still a handy construct.
The For loop is an enumeration loop with an index counting the number of repeats.
The C-style for loop is too general for the job it is usually assigned.
Real life loops are often a cross between an indexed for loop and a conditional loop.
A common problem is knowing why a loop with multiple exit points has terminated.
Anything that you can do with a loop can also be done by recursion and vice versa. However, recursion is the preferred approach if you need a variable number of nested loops.
The Trick Of The Mind - Programming & ComputationalThought
Using imitation learning, a robot has learned to perform surgical procedures as skillfully as human surgeons, bringing the field of robotic surgery closer to true autonomy.
Lightbend, the company that developed Akka, has announced Akka 3, and has changed its name to Akka. The company produces cloud-native microservices frameworks, and Akka is used for building distribute [ ... ]