For Next STEP As Variable?

Just starting out? Need help? Post your questions and find answers here.
User avatar
matalog
Enthusiast
Enthusiast
Posts: 301
Joined: Tue Sep 05, 2017 10:07 am

For Next STEP As Variable?

Post by matalog »

Is there still no way to have the integer that is required (I would suggest allowing integer variables) as the STEP in a FOR NEXT loop?

It would be a very handy thing.
BarryG
Addict
Addict
Posts: 4118
Joined: Thu Apr 18, 2019 8:17 am

Re: For Next STEP As Variable?

Post by BarryG »

Ignoring answers about Repeat/Until and While/Wend, can someone from the team please explain why a variable can't be used for the Step parameter; and also why floats can't be used? Genuinely curious as to the technicalities behind it, because you can do this in other BASICs and it means we have to adapt/convert other source codes to use Repeat/Until or While/Wend instead of just dropping in the code.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4941
Joined: Sun Apr 12, 2009 6:27 am

Re: For Next STEP As Variable?

Post by RASHAD »

I think it is been discussed and solved before
#1 :

Code: Select all

st = 9
For i = 0 To 100
  Debug i
  i + st
  f.f = i / 10
  Debug f
Next
#2 :

Code: Select all

For i = 0 To 50
  Debug i
  Read st
  i + st
Next

DataSection
  Data.i 0,1,2,3,4,5,6,7,8,9
EndDataSection

Egypt my love
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: For Next STEP As Variable?

Post by eck49 »

In at least one other BASIC, the loop variable in a simple integer For/Next loop may be held in a register, and be temporarily divorced from the value in memory. Trying to alter its value in mid-loop can have unexpected results. Can't remember which one now, but since being caught by it I've always been wary.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
BarryG
Addict
Addict
Posts: 4118
Joined: Thu Apr 18, 2019 8:17 am

Re: For Next STEP As Variable?

Post by BarryG »

eck49 wrote: Sun Aug 08, 2021 5:36 pmTrying to alter its value in mid-loop can have unexpected results
Or, expected results (if that's the desire). I've seen that before in other BASICs but can't remember why it was done. But the point still remains: even if the Step value is a variable, the coder might not be changing its value for the lifetime of the loop, so it should be allowed. PureBasic shouldn't be protecting us from ourselves in this situation (if that's the reason why it's not supported). Still waiting on an official answer.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1282
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: For Next STEP As Variable?

Post by Paul »

I don't really understand it either.
Yes you can use various way to accomplish the same thing but it doesn't make sense that
You can do this...

Code: Select all

For tmp=0 To 100 Step 5
  Debug tmp
Next
or this...

Code: Select all

#a=5
For tmp=0 To 100 Step #a
  Debug tmp
Next
but not this ?!?!?!

Code: Select all

a=5
For tmp=0 To 100 Step a
  Debug tmp
Next
Why the limitation of the hard coded constant?
Image Image
fluent
User
User
Posts: 72
Joined: Sun Jan 24, 2021 10:57 am

Re: For Next STEP As Variable?

Post by fluent »

BarryG wrote: Sun Aug 08, 2021 9:47 pm
eck49 wrote: Sun Aug 08, 2021 5:36 pmTrying to alter its value in mid-loop can have unexpected results
Or, expected results (if that's the desire). I've seen that before in other BASICs but can't remember why it was done. But the point still remains: even if the Step value is a variable, the coder might not be changing its value for the lifetime of the loop, so it should be allowed. PureBasic shouldn't be protecting us from ourselves in this situation (if that's the reason why it's not supported). Still waiting on an official answer.
IIRC this has to do with stack / heap integrity - not specific to PB.
That being said, I have never felt the need to use a variable step, and I've been writing a lot of code. Just my 2c

Also I don't think it's reasonable to expect the small PB team to read and respond to all random topics posted on the forum.
Probably better to contact support if needed. Or post in feature suggestions.
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: For Next STEP As Variable?

Post by STARGÅTE »

Paul wrote: Mon Aug 09, 2021 1:48 am I don't really understand it either.
Yes you can use various way to accomplish the same thing but it doesn't make sense that
You can do this...

Code: Select all

For tmp=0 To 100 Step 5
  Debug tmp
Next
or this...

Code: Select all

#a=5
For tmp=0 To 100 Step #a
  Debug tmp
Next
but not this ?!?!?!

Code: Select all

a=5
For tmp=0 To 100 Step a
  Debug tmp
Next
Why the limitation of the hard coded constant?
In both first and second cases, the step value is compiled into the ASM code such as "ADD dword [v_tmp], 5". Such technique is not possible in the third case and would require a change in the compiler.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: For Next STEP As Variable?

Post by eck49 »

@fluent
There is a case for a feature request, certainly, and I would guess it would not be too hard to do - but then I have never written a compiler!
@Paul
When compiling code, a constant (however specified, 5 or #a) can be built into the instruction stream, whereas a variable value first needs to be loaded from its memory address - this indirection means the compiled code runs slightly more slowly, so for the sake of both simplicity of compiler design and compiled code speed I can see why the limitation could be set. [As I was writing this, STARGATE has made the same point]

It would be a useful feature. Yes, it can be done otherwise, but For/Next/Step makes for more transparent code in my opinion.

I've wanted it for the situation where I know some action is needed from time to time but at the time of coding the frequency is not known. It may be user- or data-dependent.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
User avatar
Demivec
Addict
Addict
Posts: 4257
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: For Next STEP As Variable?

Post by Demivec »

eck49 wrote: Mon Aug 09, 2021 9:43 am @fluent
There is a case for a feature request, certainly, and I would guess it would not be too hard to do - but then I have never written a compiler!
Just for reference, here is a list of links to feature requests that are for this very thing plus one or two that include it in a list of other things:

Oct 16, 2015: Some missing functions for next release of PB
Jul 7, 2015: For:Next:Step and random() (last update: 08/11/15)
Feb 28, 2013: Variable as step value.
Sep 30, 2012: For loop using variable step.
Sep 4, 2011: STEP Variable !
May 11, 2009: A STEP forward into the future.
Feb 20, 2008: Step in For loop to accept an expression
Nov 13, 2005: Wishlist for PureBasic v4.xx - Overview
May 24, 2004: Variable after for-step
Mar 11, 2004: For ... Step ... Next ... small addition, drastic help
Jan 27, 2003: For...Next Step Variable
Dec 20, 2001: for.next.step

There doesn't seem to be any rush to implement it. Perhaps for Christmas. :)
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: For Next STEP As Variable?

Post by Marc56us »

Knowing that Fred reads everything in forum, even if it doesn't always answer, it means that this feature is not as simple to code as we think. Moreover, this lack can be circumvented in several simple ways.
As coders we all know that what seems simple to do is often very complicated and vice versa. So stop thinking that it's an oversight or a lack of willpower.
Thanks
:wink:
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: For Next STEP As Variable?

Post by eck49 »

I'm sure Marc56us is right.
As coders we all know that what seems simple to do is often very complicated and vice versa. So stop thinking that it's an oversight or a lack of willpower.
If the superficially simple has been suggested but not implemented after a long period, the reasons may be various, including some hidden complexity, a lack of demand or the lack of a simple workaround. I hope that all serious users of PB can appreciate the huge effort that Fred and others have already put in to develop the language and the work which is ongoing. What gets done in future is a matter for Fred and others to decide, and let us trust their judgement as to what is the best use of their time.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: For Next STEP As Variable?

Post by netmaestro »

Fred or Freak might answer but I wouldn't look for it as it's a topic that's been discussed to death over the last 20 years and they've never indicated that they'd consider a change here.
BERESHEIT
User avatar
chikega
User
User
Posts: 38
Joined: Fri Dec 04, 2020 3:19 am

Re: For Next STEP As Variable?

Post by chikega »

Interestingly enough I asked a similar question about the Haxe Language. I asked how would we implement this C language for-loop incrementing by 2:

Code: Select all

int main()
{
   int i;
   for (i=0; i<=10; i+=2) // increment by 2 
   {
       printf("%d\n", i);
   }
   return 0;
}
And it appears there is no option to have a variable increment (step). The suggestion was to use a while loop with the comment "I never had the need to do this with Haxe so far, so I don't miss the C-style for loop. :)".

Code: Select all

var i = 0;
while (i <= 10) {
    trace(i);
    i +=2;
}
which I translated to PureBasic

Code: Select all

b = 2   ; Step value
While a <= 10 
  Debug a
  a + b 
Wend
Gary E Chike DMD MS
'Experience is what you get when you don't get what you want' Image
Post Reply