On 05/10/12 18:58, H. S. Teoh wrote:
On Fri, Oct 05, 2012 at 05:23:40PM +0200, Don Clugston wrote:
[...]
My feeling is that  do{}while() is a fairly useless concept, and
this is part of the reason.
In my experience genuine do-while loops are extremely rare, and it
only takes a slight change to the loop to force a different
structure to be used.
Conditional loops which don't follow the while(){...} pattern
normally follow the loop-and-a-half pattern, also known as
begin-while-repeat (I think that's the name Knuth used). I'll call
it 'super do':

super do {
    foo();
    while(cond);
    bar();
}

which in D is better modelled by:

for (;;)
{
    foo();
    if (!cond) break;
    bar();
}

This isn't "super do", it's just "loop", the way nature intended. ;-)
I've always been an advocate of this construct:

        loop {
                // initial part of loop body
        } while(cond) { // exit point
                // trailing part of loop body
        }


Looks OK, except that the scopes look wrong. I would hope than a variable declared in the initial part of the body is also visible in the trailing part. The {} don't work properly. Regardless of the syntax, I think it is _the_ fundamental loop construct, and I've always found it odd that most languages don't include it. I first found encountered it in Forth, and have missed it ever since.

To some extent, D (and C/C++)'s for-loops exhibit a similar structure:

        for (X; Y; Z) {}

The trailing part of the loop body corresponds with Z; the condition
corresponds with Y.

Yes. C got 'for' loops right.

To avoid the introduction of a new keyword, we may fuse the do-loop and
the while-loop together:

        do {
                ...
        } while (cond) {
                ...
        }

The current do-loop is simply a special case of this construct where the
second {...} is replaced with a ;, and the while-loop is a special case
of this construct where the initial part of the loop is elided.

I argue that this generalized construct is much more useful than the
do-loop or while-loop individually, plus it doesn't break any existing
code.

I agree that it's more useful. But that code was legal until a couple of releases ago, because a trailing ; was not required on do-while loops.

do { xxx; } while(cond) { yyy; }

means:
do {
  xxx;
}
while(cond);
yyy;

Even without that, it puts a huge significance on that semicolon. So I don't think that works. How about:

do {  ...  do while (cond);  ... }

?
This is technically already legal too, although 'do while(cond);' is currently either a no-op, or an infinite loop.

Reply via email to