foreach

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 13 15:06:17 PDT 2014


On Fri, Jun 13, 2014 at 11:41:34PM +0200, Jonathan M Davis via Digitalmars-d wrote:
> 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.

It's a philosophical question whether a missing loop condition means
"the loop never stops" or "the loop never runs". So obviously reasonable
people are going to disagree on this. The way I see it, the "loop never
runs" option is untenable, because the very fact that the loop was
written in the first place means that the programmer intends for
something to loop, rather than for nothing to happen at all. Thus, the
most reasonable interpretation of a condition-less loop, in my mind, is
that it will unconditionally loop.

I don't see it as "adding" true or false in place of the missing loop
condition at all -- I consider that patchwork thinking (going back after
the fact to insert something that was never there to begin with) rather
than getting into the real issue, which is, how should a conditionless
loop be understood? Or, should I say, an unconditional loop, the very
wording of which suggests that the right answer is a loop that never
stops.


> > > 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.

Why would it be "very, very wrong"? Perpetual cycles are ubiquitous in
nature -- the spin of an electron, the crests of a standing wave, the
cycle of day and night, the orbit of the Earth around the Sun, etc..
They are the most natural of loops! Now, one may argue that day/night
cycles and the orbit of the Earth aren't *truly* perpetual cycles since
it's unlikely the Solar System will continue to exist forever, so they
may one day in the remote future cease, but that in itself underlines
the point that while an infinite loop is *intended* to go on forever --
like a webserver that infinitely repeats the receive-request and
deliver-response cycle -- whether it will *actually* never terminate is
a different question (user interrupt, admin shutdown, server crash,
power outage, heat death of the universe, or the code throws an
Exception or has some exit condition that breaks the loop under some
circumstances, etc.).

I don't see anything *inherently* wrong with an unconditional (i.e.
infinite) loop, except where human error introduces it where it
shouldn't be. I mean, surely a webserver that continually serves web
requests should run forever -- or at least as far as it can go? I can't
imagine someone writing an actual loop condition for a webserver's main
loop, for example!

It's only because of human error introducing these infinite loops where
they shouldn't be, that gives the term "infinite loop" a negative
connotation. Essentially speaking, there's nothing *inherently* wrong
with a loop running forever. Just don't use it when you don't *intend*
for it to run forever!


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

Sure, but why stop when we can in(de)finitely (har har) extend threads
involving such trivialities as bikeshedding on for(;;) vs. while(1),
instead of discussing more important issues like semantics and the cost
of the nuclear reactor? ;-)


T

-- 
Stop staring at me like that! It's offens... no, you'll hurt your eyes!


More information about the Digitalmars-d mailing list