Re: How to change the increment of FOR loop? [message #65589] |
Thu, 12 March 2009 05:24  |
Allan Whiteford
Messages: 117 Registered: June 2006
|
Senior Member |
|
|
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.
Thanks,
Allan
|
|
|
Re: How to change the increment of FOR loop? [message #65602 is a reply to message #65589] |
Wed, 11 March 2009 14:42   |
Hu
Messages: 35 Registered: January 2009
|
Member |
|
|
On Mar 11, 4:24 pm, FÖLDY Lajos <fo...@rmki.kfki.hu> 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
thank you very much
wishes
|
|
|
Re: How to change the increment of FOR loop? [message #65603 is a reply to message #65602] |
Wed, 11 March 2009 14:24   |
Foldy Lajos
Messages: 268 Registered: October 2001
|
Senior Member |
|
|
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
|
|
|
Re: How to change the increment of FOR loop? [message #65660 is a reply to message #65589] |
Fri, 13 March 2009 03:31  |
Foldy Lajos
Messages: 268 Registered: October 2001
|
Senior Member |
|
|
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
|
|
|