For Next STEP As Variable?
For Next STEP As Variable?
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.
It would be a very handy thing.
Re: For Next STEP As Variable?
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.
Re: For Next STEP As Variable?
I think it is been discussed and solved before
#1 :
#2 :
#1 :
Code: Select all
st = 9
For i = 0 To 100
Debug i
i + st
f.f = i / 10
Debug f
Next
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
Re: For Next STEP As Variable?
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)
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
Re: For Next STEP As Variable?
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.eck49 wrote: Sun Aug 08, 2021 5:36 pmTrying to alter its value in mid-loop can have unexpected results
Re: For Next STEP As Variable?
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...
or this...
but not this ?!?!?!
Why the limitation of the hard coded constant?
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
Code: Select all
#a=5
For tmp=0 To 100 Step #a
Debug tmp
Next
Code: Select all
a=5
For tmp=0 To 100 Step a
Debug tmp
Next
Re: For Next STEP As Variable?
IIRC this has to do with stack / heap integrity - not specific to PB.BarryG wrote: Sun Aug 08, 2021 9:47 pmOr, 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.eck49 wrote: Sun Aug 08, 2021 5:36 pmTrying to alter its value in mid-loop can have unexpected results
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.
Re: For Next STEP As Variable?
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.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...or this...Code: Select all
For tmp=0 To 100 Step 5 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?Code: Select all
a=5 For tmp=0 To 100 Step a Debug tmp Next
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 more ― Typeface - Sprite-based font include/module
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
Re: For Next STEP As Variable?
@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.
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)
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
Re: For Next STEP As Variable?
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: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!
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.

Re: For Next STEP As Variable?
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

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

Re: For Next STEP As Variable?
I'm sure Marc56us is right.
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.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.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: For Next STEP As Variable?
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
Re: For Next STEP As Variable?
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:
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.
".
which I translated to PureBasic
Code: Select all
int main()
{
int i;
for (i=0; i<=10; i+=2) // increment by 2
{
printf("%d\n", i);
}
return 0;
}

Code: Select all
var i = 0;
while (i <= 10) {
trace(i);
i +=2;
}
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'
'Experience is what you get when you don't get what you want'
