Jump to content

Loop-and-a-half: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Much better URL for the paper.
No edit summary
Line 25: Line 25:
</source>
</source>


The 'half' refers to the fact that only part of the last loop iteration is performed <ref>{{cite web|url=http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.103.6084|title=Structured Programming With GOTO Statements|author=[[Donald Knuth]] (quoting [[Edsger_W._Dijkstra]])|year=1974}}</ref>.
The 'half' refers to the fact that only part of the last loop iteration is performed <ref>{{cite web|url=http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.103.6084|title=Structured Programming With GOTO Statements|author=[[Donald Knuth]] (quoting [[Edsger_W._Dijkstra]])|year=1974}}</ref><ref>{{cite web|url=https://books.google.com/books?id=LpVCAwAAQBAJ&pg=PA369&dq=loop+and+a+half&hl=en&sa=X&redir_esc=y#v=onepage&q=loop%20and%20a%20half&f=false|title=Code Complete|author=Steve McConnell}}</ref>.
Further solutions variants include fetching the next item and immediately testing for the sentinel (in [[C_(programming_language)|C]]):
Further solutions variants include fetching the next item and immediately testing for the sentinel (in [[C_(programming_language)|C]]):



Revision as of 18:39, 4 December 2018

Loop-and-a-half is a computer science term for a control flow construct where a loop is exited in the middle, instead of at the beginning or at the end. This kind of logic is needed for example when processing an unknown number of data items until a certain condition becomes true indicating that there are no more items to process. Usually the exit condition has to do with the data items, for example looking for a particular item called the sentinel which indicates the end of the items.

Writing a loop-and-a-half in a simplistic manner leads to code duplication which is a problem for software maintenance and hard to understand logic which again is a burden on software maintenance.

In the below examples we read data items from a file stream and test against a known sentinel value.

item = read()
while item is not sentinel:
   process(item)
   item = read()

The fetching of the next item is duplicated, and the two instances of it are separated by the potentially long processing for the data item. This leads into brittle and error-prone code.

The basic solution is to use a loop that looks like an infinite loop but is not, because in case of the sentinel we exit the loop:

while true:
   item = read()
   if item is sentinel:
       break
   process(item)

The 'half' refers to the fact that only part of the last loop iteration is performed [1][2]. Further solutions variants include fetching the next item and immediately testing for the sentinel (in C):

while ((item = read()) != sentinel) {
   process(item);
}

or using a pseudo-infinite do-while loop which is equivalent with the above pseudo-infinite while loop:

do {
   item = read();
   if (item == sentinel) {
       break;
   }
   process(item);
} while (true);

Some purists of structured programming object to leaving loops in the middle, but following that path means that the state of whether to stay in the loop or continue processing has to be maintained separately, again leading into brittle code.

References

  1. ^ Donald Knuth (quoting Edsger_W._Dijkstra) (1974). "Structured Programming With GOTO Statements".
  2. ^ Steve McConnell. "Code Complete".