Page 1 of 3
A STEP forward into the future.
Posted: Mon May 11, 2009 8:49 pm
by akj
I have requested this before, but never received any reply.
The feature I miss most with PureBasic compared with other basics is the inability for the STEP value in a FOR/NEXT loop to be a variable or expression rather than a constant. It is a real pain that this fundamental core feature is missing and the published workarounds have unwanted side effects in critical cases.
Is there any chance of this feature being incorporated into PB 4.40 which is due in the near future?
Posted: Mon May 11, 2009 9:15 pm
by Saboteur
I think While/Wend is easiest for that.
What do you need to do in a loop?
Posted: Mon May 11, 2009 9:22 pm
by akj
@Saboteur:
But the While condition is dependent on whether the STEP value is positive or negative (this may not be known in advance) and there is no builtin SGN() function in PB so it all starts getting rather messy.
Posted: Mon May 11, 2009 10:37 pm
by Mistrel
+1, but I do acknowledge that the cases where I've used it are very rare.
Posted: Mon May 11, 2009 10:45 pm
by Kaeru Gaman
the FOR-Loop in PB is a very specialized Loop for performance reasons.
for this, step is a constant, and the loop variable is an integer of register size.
Posted: Mon May 11, 2009 10:57 pm
by ts-soft
- 1
Use While Wend or Repeat Until, but please, don't slow down the For Next
with Runtime interpretet Step, is useless.
For Next is a speed optimized Integer Counter, nothing else
Posted: Mon May 11, 2009 11:11 pm
by akj
@ts-soft:
The For/Next loop performance will not decrease as the compiler will simply generate code appropriate to whether the STEP value is constant or not (or absent).
Posted: Tue May 12, 2009 12:54 am
by Mistrel
akj wrote:@ts-soft:
The For/Next loop performance will not decrease as the compiler will simply generate code appropriate to whether the STEP value is constant or not (or absent).
I was thinking the same thing.
Posted: Tue May 12, 2009 1:02 am
by freak
> But the While condition is dependent on whether the STEP value is positive or negative (this may not be known in advance)
It would be the same if the step supported variables.
Posted: Tue May 12, 2009 1:10 am
by akj
@freak:
Yes, but the compiler would take care of the complications once and for all, without the programmer having to reinvent the wheel each time the full power of For/Step/Next was needed.
Posted: Tue May 12, 2009 1:27 am
by Kaeru Gaman
how should the compiler take care of it?
it would also don't know while compile if the STEP variable is positive or negative.
I think there are much more important tasks than screwing the For-Loop for a doubtable result...
Posted: Tue May 12, 2009 1:33 am
by Kaeru Gaman
akj wrote:But the While condition is dependent on whether the STEP value is positive or negative (this may not be known in advance) and there is no builtin SGN() function in PB so it all starts getting rather messy.
you can Flag the End condition out...
Code: Select all
LoopStart = Random(99)
Repeat
LoopStop = Random(99)
Until LoopStop <> LoopStart
LoopStep = Random(9)+1
If LoopStart > LoopStop
LoopStep = -LoopStep
EndIf
Debug LoopStart
Debug LoopStop
Debug LoopStep
Debug "----------"
LoopCount = LoopStart
Repeat
Debug LoopCount
LoopCount + LoopStep
If LoopStep < 0
If LoopCount < LoopStop
LoopEnd = #True
EndIf
Else
If LoopCount > LoopStop
LoopEnd = #True
EndIf
EndIf
Until LoopEnd
this should be the fastest and easiest solution...
Posted: Tue May 12, 2009 3:50 am
by pdwyer
+1, if you want to use a constant for speed purposes then use one. this has been standard in the basic languages I've used.
While I rarely want to change a step value mid loop, I often want to call the loop in a function and pass the step value across for that loop iteration. Often the step is a modulo of the loop size to give rows etc.
Posted: Tue May 12, 2009 9:04 am
by Mistrel
pdwyer wrote:While I rarely want to change a step value mid loop, I often want to call the loop in a function and pass the step value across for that loop iteration. Often the step is a modulo of the loop size to give rows etc.
An excellent example, pdwyer. I wish I could think of something. I know I've run into this before.
Posted: Tue May 12, 2009 9:30 am
by djes
pdwyer wrote:+1, if you want to use a constant for speed purposes then use one. this has been standard in the basic languages I've used.
While I rarely want to change a step value mid loop, I often want to call the loop in a function and pass the step value across for that loop iteration. Often the step is a modulo of the loop size to give rows etc.
+1