foreach

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 13 14:41:34 PDT 2014


On Fri, 13 Jun 2014 11:03:14 -0700
"H. S. Teoh via Digitalmars-d" <digitalmars-d at puremagic.com> wrote:

> On Fri, Jun 13, 2014 at 10:46:17AM -0700, Jonathan M Davis via
> Digitalmars-d wrote: [...]
> > for(;;) is a special case with no real benefit IMHO. It's a loop whose
> > condition is implicitly true rather than actually having a condition
> > in it.  IMHO, it should required to be at least for(;1;) or
> > for(;true;), since those don't require a special case.
>
> I disagree, it's not a special case. It's simply a logical consequence
> of each part of the for-loop being optional. Prohibiting for(;;) would
> *be* a special case, because then you're saying that each component of
> the for-loop is optional, *except* when all of them are omitted.
>
> (Not to mention, for(;1;) is truly an eyesore, far worse than for(;;).)

It's a special case in that the middle portion is supposed to be the condition
that the loop use to determine whether it can continue, and omitting it means
that it has to add the true itself, whereas with the other two pieces it makes
perfect sense that they'd be optional, since they're not required to determine
whether the loop needs to terminate. They're just handy helpers.

> > And if that's what you're doing, you might as well just use while(1),
> > since there's no point in using for over while if you're not doing
> > anything in the other two parts of the for loop.
>
> Again I disagree. Using for(;;) is completely natural, because the
> definition of for() says that each of the 3 parts are optional. Since an
> infinite loop loops *unconditionally*, it doesn't have any loop
> conditions, so the most natural thing to do is to use a construct where
> the loop condition can be omitted -- i.e., for(;;).
>
> On the contrary, using while() here is unnatural because while() expects
> a loop condition, but since an infinite loop doesn't have one, you have
> to artificially invent a constant value to stick into the loop condition
> in order to satisfy the syntax of the while-loop. I find this to be
> quite unnatural.

The very idea of a loop without a condition seems very, very wrong to me.

But clearly, we're not going to agree on this.

- Jonathan M Davis


More information about the Digitalmars-d mailing list