For : Next step

Everything else that doesn't fall into one of the other PB categories.
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

For : Next step

Post by Lunasole »

This is more philosophical question ^^, why only constant is allowed to be used as "Step"?
Can there be some special reasons for that (useless) limitation?
"add dword [A], 0x5" might be a bit faster of course than adding , but it doesn't seems to make any difference, especially if both are allowed and related variation of asm-code used.

Code: Select all

Define B = 5
For A = 1 To 10 Step B ; not allowed
	A + 1
Next A
PS. I don't see a big problem with that, While/Repeat can be used easily as alternative, just found it curious.
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: For : Next step

Post by Keya »

Would be nice, i needed it myself not long ago. My workaround was something like this, granted it's not as readable!

Code: Select all

B = 5
Repeat
  A + B   ;A Step B
  Debug(A)
Until A => 10
User avatar
idle
Always Here
Always Here
Posts: 6018
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: For : Next step

Post by idle »

if you really want the syntax it can be done at the expense of 3 ops

Code: Select all

CompilerIf SizeOf(Integer) = 4 
  Macro rcx : ecx : EndMacro 
CompilerEndIf   

Macro NextStep(var,istep=1)
  EnableASM 
  mov rcx, istep 
  dec rcx 
  add var, rcx
  DisableASM
  Next 
EndMacro 

Define b = 2

For A = 1 To 20 
  Debug A
NextStep(A,b)  
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
TI-994A
Addict
Addict
Posts: 2744
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: For : Next step

Post by TI-994A »

Keya wrote:My workaround was something like this, granted it's not as readable!

Code: Select all

B = 5
Repeat
  A + B   ;A Step B
  Debug(A)
Until A => 10
If we're digressing from your philosophical question into workarounds, then this maintains syntax:

Code: Select all

Define Stepp = 5
For A = 1 To 10 Step 0
  loopCount + 1
  Debug loopCount
  A + Stepp
Next A
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
sys64802
Enthusiast
Enthusiast
Posts: 105
Joined: Sat Sep 12, 2015 6:55 pm

Re: For : Next step

Post by sys64802 »

Lunasole wrote: Can there be some special reasons for that (useless) limitation?
No, not really. Dartmouth College Basic supported it in 1964 BTW :)
s0ula55a551n
User
User
Posts: 25
Joined: Fri Jan 01, 2016 5:55 pm

Re: For : Next step

Post by s0ula55a551n »

I found this annoying also and can't see good reason why it has to be constant. Most version of basic I have used allow you to set the step using a variable
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: For : Next step

Post by walbus »

But, it is very simple for setting the stepping inside any loop....
s0ula55a551n
User
User
Posts: 25
Joined: Fri Jan 01, 2016 5:55 pm

Re: For : Next step

Post by s0ula55a551n »

Yes you can obviously step the loop counter to a variable. But why not just allow it on the step
User avatar
TI-994A
Addict
Addict
Posts: 2744
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: For : Next step

Post by TI-994A »

s0ula55a551n wrote:...why not just allow it on the step
In short, Step values in For/Next loops are resolved at compile time, and not at run time.

This is purely a design consideration for improved speed. TTBOMK. :wink:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
sys64802
Enthusiast
Enthusiast
Posts: 105
Joined: Sat Sep 12, 2015 6:55 pm

Re: For : Next step

Post by sys64802 »

It's just a limitation. You could have both.
The compiler could add the constant to the loop variable using an immediate operand when a literal constant is specified (like it does now), and add the content of a variable using an intermediate register when a variable is specified instead.
Different code generation, in each case the more appropriate to translate the source code, it's what compilers do.
Obviously one construct can be slower than another, but that happens constantly when programming.
The job of the language should be to be expressive and powerful, the job of the compiler should be to generate the best possible code for the given source, the job of the programmer should be to make the more appropriate choices when putting together the source (and sometimes even sub-optimal choices can still be fixed by the compiler, but that's beyond our world here and it would be excessive to expect that).
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: For : Next step

Post by Danilo »

User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: For : Next step

Post by Tenaja »

sys64802 wrote:
Lunasole wrote: Can there be some special reasons for that (useless) limitation?
No, not really.
Actually, that is not true. The "special reason" is that 99% of us would rather the dev team worked on more important issues, since this feature can be coded with a Repeat loop. (And besides, there are numerous macros available to implement it using FOR-like syntax, if you search.)
sys64802
Enthusiast
Enthusiast
Posts: 105
Joined: Sat Sep 12, 2015 6:55 pm

Re: For : Next step

Post by sys64802 »

Actually, that's not the reason, there can't be a real reason for that beyond the decision of not doing it at the time. Maybe it was not considered important.
And sure, it's not very important. It's just a nice convenient thing to have.
It's a decision like another, even if in this case we are talking about something minimal, not adding classes.
But I will not insist further, in my opinion there's nothing more to say about something so simple.
The question was "Can there be some special reason" ?
I still think a sensible answer may be "no, not really" and not "to give you more speed" or "to use that time to implement some other feature".
And he already knew the answer, judging by what he wrote.
Post Reply