Re: IDL FOR Loop variable increments [message #62607 is a reply to message #62605] |
Thu, 18 September 2008 15:46   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Jean H writes:
>> I think you have clearly chosen the wrong program
>> construction. You should NEVER be changing a loop
>> variable inside the loop programmatically. (I don't
>> think IDL 7 even allows it, since I was inadvertently
>> doing it the other day and got my hand slapped.) Maybe
>> you want a WHILE loop.
>
> No problem to do that here..
> { x86 Win32 Windows Microsoft Windows 7.0.1 Mar 20 2008 32 64}
Humm. I was afraid of that. I was too much in the middle
of something to stop and see what the real problem was.
I was passing a loop variable into another program as a
parameter, and that other program was, on occasion, changing
the passed-in variable to something else, based on some
programming logic. I was getting some kind of error about
"cannot change this kind of variable".
I don't know. I've never seen the error before, and I can't
reproduce it now in a quick test program, so... Probably
didn't happen. :-)
> Could you comment on the "risk" of changing the loop counter within the
> loop?
I was in a hurry and late for a meeting when I wrote this,
but as I was going on the door, I realized that of course
we often change "loop variables" inside of loops. In fact,
the loop I suggested, a WHILE loop, nearly *always* does that.
So, nonsense.
The point I was trying to make, though, is that in
a counting loop, like a FOR loop, it seems to me that
changing the counter in the program is a terrible idea.
I just can't think of any time it is a good idea. I think
it is a programming style that just flat out leads to
programming errors. If you think you have to do it, then
I think you ought to re-think what you are doing, and choose
another programming structure that lends itself more easily
to the task.
I've just never seen anything good come of it.
Cheers,
David
P.S. Let's just say that since I now have to work for a living, I
find there is a LOT less time to actually think about what I am
writing. I am MUCH more sympathetic to the kind of advice that
is typical on any newsgroup except this one. :-)
--
David Fanning, Ph.D.
Coyote's Guide to IDL Programming (www.dfanning.com)
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|