POV-Ray : Newsgroups : povray.pov4.discussion.general : I want to be rid of my stupid mistakes : Re: I want to be rid of my stupid mistakes Server Time
11 Jun 2024 14:12:02 EDT (-0400)
  Re: I want to be rid of my stupid mistakes  
From: clipka
Date: 21 Jul 2009 22:45:00
Message: <web.4a667d2797aca271785322500@news.povray.org>
"Trevor G Quayle" <Tin### [at] hotmailcom> wrote:
> I really dont see the need for a FOR loop.  In the most general sense, FOR loops
> just increment from one value to another at a preset interval.  FOR loops are
> essentially just specialized WHILE loops and very easy to implement in POV
> without causing errors.

From a theoretical point of view you're perfectly right, but from a practical
point of view you're absolutely wrong.

The most common case of loop construct used in POV-Ray SDL is the standard
countng loop:

  #declare i = 1;
  #while (i <= 10000)
    //...
    #declare i = i + 1;
  #end

There is a lot of unnecessary typing involved here, and some of it is too easily
forgotten; as an aside, the loop construct, although perfectly documenting the
technical workings of the loop, doesn't make the programmer's original
intentions all too obvious, because it "atomizes" the whole loop into many
separate statements. The "#declare i = 1;" can easily get lost among other loop
initialization statements, and the "#declare i = i + 1;" can easily get lost
among the loop body. It's also quite inconsistent: Why would the start of the
range over which to loop be mentioned in a "#declare" statement, while only the
end of the range would be mentioned in the loop statement itself?

On the other hand, something like

  #for (i = 1 to 10000)
    //...
  #end

is much more concise, avoids unnecessary typing (no typing the loop variable 4
times and typing #declare twice, as in the #while version), shows better what's
going on here, is consistent in mentioning both start and end of the number
range in the loop statement itself, and - best of all - avoids the
all-too-common mistake of forgettig to increment.

If you think the #while loop is good enough, feel free to continue to use it,
but please - just because *you* don't need it, don't hinder others from getting
it. The current #while loop *is* a PITA for quite some folks, me included.


> FOR i = 1 to 6 STEP j
>   ...
> NEXT j
>
> #declare i=1; #while (i<=6)
>   ...
> #declare i=i+j; #end

Interestingly, you made all attempts to "compactify" the #while code, but not so
with the FOR code. How come?

Now give that loop variable a more verbose name, say CurrentIdx:

  #for (CurrentIdx = 1 to 6 step j)
    // ...
  #end

or

  #declare CurrentIdx = 1; #while (CurrentIdx <= 6)
    // ...
  #declare CurrentIdx = CurrentIdx + 1; #end

I know which version I'd prefer.

Also note that your version of trying to integrate the initialization and
increment into the loop itself leaves you with the "#while" and "#end" right in
the middle and the end of a line, respectively, making it difficult to visually
identify and match these statements quickly in the code.

> Most of the infinite loop situations that have been presented would not be
> solved properly with a FOR loop.  For example, any of the conditional increment
> type loops (counter is increased only if a certain condition is met) require
> extra steps in a FOR loop to work:

There's a difference here between statements that commonly create infinite
loops, and statements that an infinite-loop detector would have to be able to
properly cope with.

The loop that counts each and every iteration is *the* standard case, and also
*the* standard reason for undesired infinite loops. That's *the* classic use
case for a for-loop. If you want to count up only in certain cases, you're much
less likely to forget it - and it's also comparatively uncommon; for these
cases, #while-loops would continue to be the weapon of choice.


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.