The Trick Of The Mind - The Loop Zoo
Written by Mike James   
Monday, 03 April 2023
Article Index
The Trick Of The Mind - The Loop Zoo
Loops That End
Fractional Loops

Fractional Loops

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

Buy Now From Amazon

Trick360

Chapter List

  1. The Trick Of The Mind

  2. Little Languages
       Extract: Little Languages Arithmetic

  3. Big Languages Are Turing Complete

  4. The Strange Incident of The Goto Considered Harmful
       Extract: The Goto Considered Harmful

  5. On Being Variable  

  6. Representation

  7. The Loop Zoo
       Extract The Loop Zoo
      
    Extract Advanced Loops

  8. Modules, Subroutines, Procedures and Functions
       Extract Modular Programming

  9. Top-Down Programming 

  10. Algorithms
       Extract: Binary Search 
       Extract: Recursion ***NEW!!

  11. The Scientific Method As Debugging 

  12. The Object Of It All
       Extract Why Objects 

 <ASIN:1871962722>

<ASIN:B09MDL5J1S>

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Flutter Forked As Flock
05/11/2024

One of developers who worked on the Flutter team at Google has created an open-source form of the framework. Matt Carroll says Flock will be "Flutter+", will remain constantly up to date with Flutter, [ ... ]



Raspberry Pi CM5 - Expensive And Undocumented
27/11/2024

So the unexpected has happened - the Compute Module 5 has been launched. But it simply emphasises some problems with adopting the Pi as an IoT device.


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Tuesday, 04 April 2023 )