Page 1 of 1

For Next STEP As Variable?

Posted: Sat Aug 07, 2021 12:51 am
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.

Re: For Next STEP As Variable?

Posted: Sat Aug 07, 2021 1:45 am
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.

Re: For Next STEP As Variable?

Posted: Sat Aug 07, 2021 2:02 am
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


Re: For Next STEP As Variable?

Posted: Sun Aug 08, 2021 5:36 pm
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.

Re: For Next STEP As Variable?

Posted: Sun Aug 08, 2021 9:47 pm
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.

Re: For Next STEP As Variable?

Posted: Mon Aug 09, 2021 1:48 am
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?

Re: For Next STEP As Variable?

Posted: Mon Aug 09, 2021 8:27 am
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.

Re: For Next STEP As Variable?

Posted: Mon Aug 09, 2021 9:15 am
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.

Re: For Next STEP As Variable?

Posted: Mon Aug 09, 2021 9:43 am
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.

Re: For Next STEP As Variable?

Posted: Mon Aug 09, 2021 11:16 am
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. :)

Re: For Next STEP As Variable?

Posted: Mon Aug 09, 2021 12:41 pm
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:

Re: For Next STEP As Variable?

Posted: Tue Aug 10, 2021 12:34 pm
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.

Re: For Next STEP As Variable?

Posted: Fri Aug 13, 2021 5:57 pm
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.

Re: For Next STEP As Variable?

Posted: Wed Aug 18, 2021 5:48 am
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