loop-and-a-half

<programming>

A loop structure with its termination test in the middle. This contrasts with the more common while loop which has the test at the beginning or a repeat loop which has it at the end.

A generic example gets a value, tests it then processes it:

 while (true) {
   value = get();
   if (isTerminal(value)) break;
   process(value);
 }

Alternatives require either an extra get() before the loop:

 value = get();
 while (! isTerminal(value))
   process(value);
   value = get();
 }

or an extra test for termination:

 do {
   value = get();
   done = isTerminal(value);
   if (! done) process(value);
 } while (! done);

The structure is discussed, though not named as such, in

[Donald Knuth, "Structured programming with goto statements", Computing Surveys, December 1974].

A good summary is presented in

[Eric S. Roberts, "Loop Exits and Structured Programming: Reopening the Debate", ACM SIGCSE, March 1995].

Last updated: 2019-09-03

Nearby terms:

LOOKSlooploop-and-a-halfloop combinationloop fusionLOOPN

Try this search on Wikipedia, OneLook, Google


Loading