comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » How to change the increment of FOR loop?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
How to change the increment of FOR loop? [message #65605] Wed, 11 March 2009 14:02 Go to next message
Hu is currently offline  Hu
Messages: 35
Registered: January 2009
Member
Hi, there
I got a question relating to the variational increment of FOR
Statement. I used the code like this:
;;; begin
increment =20
FOR j = 0, N-1, increment DO BEGIN
IF a gt 0 THEN BEGIN
;......
increment=10
ENDIF ELSE BEGIN
;......
increment=12
ENDELSE
ENDFOR
;; end

But the increment have not changed according to IF... ELSE ...
statement..
What I want is to change the increment according to different
conditions. that is, if variable a is greater than 0, the increment of
the FOR loop will be 10 , not its default value 20.....

Is there any solution to this problem?
Re: How to change the increment of FOR loop? [message #65658 is a reply to message #65605] Fri, 13 March 2009 06:44 Go to previous message
Allan Whiteford is currently offline  Allan Whiteford
Messages: 117
Registered: June 2006
Senior Member
F�LDY Lajos wrote:
>
> On Thu, 12 Mar 2009, Allan Whiteford wrote:
>
>> F�LDY Lajos wrote:
>>
>>>
>>> On Wed, 11 Mar 2009, Hu wrote:
>>>
>>>> Hi, there
>>>> I got a question relating to the variational increment of FOR
>>>> Statement. I used the code like this:
>>>> ; ;; begin
>>>> increment =20
>>>> FOR j = 0, N-1, increment DO BEGIN
>>>> IF a gt 0 THEN BEGIN
>>>> ;......
>>>> increment=10
>>>> ENDIF ELSE BEGIN
>>>> ;......
>>>> increment=12
>>>> ENDELSE
>>>> ENDFOR
>>>> ;; end
>>>> > But the increment have not changed according to IF... ELSE ...
>>>> statement..
>>>> What I want is to change the increment according to different
>>>> conditions. that is, if variable a is greater than 0, the
>>> increment of
>>>> the FOR loop will be 10 , not its default value 20.....
>>>> > Is there any solution to this problem?
>>>
>>>
>>> increment is evaluated only once, before the loop starts (and it is
>>> copied
>>> to a temporary variable you can not reach). You have to create a loop
>>> manually:
>>>
>>> j=0
>>> increment=20
>>> while 1 do begin
>>>
>>> IF a gt 0 THEN BEGIN
>>> ;......
>>> increment=10
>>> ENDIF ELSE BEGIN
>>> ;......
>>> increment=12
>>> ENDELSE
>>>
>>> j=j+increment
>>> if j ge n then break
>>>
>>> endwhile
>>>
>>>
>>> regards,
>>> lajos
>>
>>
>> Although probably something like:
>>
>> j=0
>> increment=20
>> while ((j+=increment) lt n) do begin
>>
>> IF a gt 0 THEN BEGIN
>> ;......
>> increment=10
>> ENDIF ELSE BEGIN
>> ;......
>> increment=12
>> ENDELSE
>>
>> endwhile
>>
>> is easier to recognise as a "loop" from the point of view of
>> understanding the code later for maintenance.
>
>
> It is not the same loop :-) j starts from increment insted of 0. In this
> form you should use j=-increment.
>
> regards,
> lajos

Pah, details - so long as the step size is small enough the start point
hardly ever matters :).

Seriously, though, good point - well made.

Corrected code posted here for future reference:

increment=20
j=-increment
while ((j+=increment) lt n) do begin

IF a gt 0 THEN BEGIN
;......
increment=10
ENDIF ELSE BEGIN
;......
increment=12
ENDELSE

endwhile

although it doesn't look nearly as nice as it did I still think a loop
should look like a loop to make it easier to parse with your eyes.

I guess one could use repeat...until to get rid of the above issue but
that's a bit ugly - I prefer the loop construct to appear at the top.

Thanks,

Allan
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: eclipse and mercurial
Next Topic: WIDGET COLORS

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:16:18 PDT 2025

Total time taken to generate the page: 0.08187 seconds