A STEP forward into the future.

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

A STEP forward into the future.

Post 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?
Anthony Jordan
Saboteur
Enthusiast
Enthusiast
Posts: 272
Joined: Fri Apr 25, 2003 7:09 pm
Location: (Madrid) Spain
Contact:

Post by Saboteur »

I think While/Wend is easiest for that.
What do you need to do in a loop?
[:: PB Registered ::]

Win10 Intel core i5-3330 8GB RAM Nvidia GTX 1050Ti
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post 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.
Anthony Jordan
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

+1, but I do acknowledge that the cases where I've used it are very rare.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post 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
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post 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).
Anthony Jordan
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post 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.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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.
quidquid Latine dictum sit altum videtur
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post 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.
Anthony Jordan
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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...
oh... and have a nice day.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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...
oh... and have a nice day.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post 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.
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post 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.
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Post 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
Post Reply