foreach

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Sat Jun 14 14:55:02 PDT 2014


On Sat, Jun 14, 2014 at 02:23:36PM -0700, Jonathan M Davis via Digitalmars-d wrote:
> On Sat, 14 Jun 2014 10:20:51 +0000
> via Digitalmars-d <digitalmars-d at puremagic.com> wrote:
[...]
> > But this special treatment of the second operand is the same in all
> > forms of the for loop:
> >
> >      for(int i = 0; ; ++i)    // endless loop
> >
> > Therefore, the fact that `for(;;)` is also an infinite loop is not
> > an exception.
> 
> Regardless of whether there's anything in the other two parts of the
> loop, it's still ommitting the condition, which IMHO is a special case
> and incredibly bizarre. Rather than the compiler having a condition to
> check, suddenly it has to know that it needs to essentially insert true
> for the condition, because the condition isn't there. This is in
> contrast to the other two parts of the loop which are handy places to
> put statements, and having a line with no statement isn't special at
> all.

I don't understand what's the hang-up with "insert true for the
condition". I think that's the wrong way to look at it. The compiler
isn't "inserting" anything for the condition -- there simply *isn't* any
condition to begin with! At the machine level, implementing a loop is as
simple as a single branch instruction back to a previous point in the
program. When the loop has a condition, then a condition check has to be
inserted before the branch in order to decide whether to take the branch
or not. In this sense, the condition-less loop is more basic, or
fundamental, because it's conceptually (and implementationally) simpler.
It is loops *with* conditions that are conceptually more complex.
Therefore, IMO thinking of "inserting true as a condition" is looking at
things backwards. Rather, one starts with the most bare-bones, basic
construction, which is an infinite loop (i.e., a loop without a
condition), to which one *adds* a condition when the loop is to exit at
some future point.

IOW, there is no "inserting true in the condition" happening at all.
The fundamental loop simply has no such thing as a condition to begin
with.  There is no condition in which to "insert a true". Rather, the
loop in its most basic form just loops (forever), and it's the act of
writing a condition in the for(;...;) construct that extends the idea of
the loop to include a stopping condition.


> It's the lack of a condition that I object to. IMHO it's a fundamental
> violation of how loops work.
[...]

I guess this is where we disagree on a fundamental level. Your concept
of "loop" is that the loop condition is an inherent property of loops.
My concept of "loop" is that it is simply a loop and nothing else --
it's just the repetition of a block of code. Nothing is said about under
what conditions it will continue iteration. Loop conditions are
something added to this fundamental construct in order to make the loop
finite in number of iterations. Thus, for(;;) represents the most
fundamental essence of a loop -- it just loops forever. Adding code for
any of its 3 parts constitute an extension of this most fundamental
construct -- i.e., adding initialization code, adding a stopping
condition, adding a post-iteration increment. So it is the *presence* of
a condition that represents the more derived concept, rather than the
absence of a condition requiring the artificial insertion of a constant
true condition.

Your concept of loops is more akin to a while-loop, which by its nature
*requires* a condition as part of its definition. Thus, in order to make
it run forever, one has to artificially insert a constant true value in
its condition. Because of this, infinite loops are fundamentally a
foreign in the context of a while-loop: the while-loop was designed to
stop after some condition is fulfilled, yet now we wish to suppress this
stoppage, therefore we need to insert a constant true condition to it.
Conceptually speaking, however, going back to suppress something feels
more unnatural than using something that doesn't need anything to be
suppressed in the first place. That's why I prefer writing for(;;) for
infinite loops rather than while(1) -- the 1 in while(1) represents an
artifical kludge to make a fundamentally finite loop become infinite,
whereas for(;;) simply uses the concept of a loop at its most
fundamental level -- repetition of the code block with nothing said
about stopping conditions -- and thus represents the most direct
expression of the idea of an infinite loop.


T

-- 
No! I'm not in denial!


More information about the Digitalmars-d mailing list