For Next Step
Re: For Next Step
What is the overarching reason for requiring that the target of STEP be a constant?
Re: For Next Step
The only reason would be simplicity. That's the way Fred coded it originally, and he found no compelling reason to change it. Using a variable STEP wold require extra code edits.
Re: For Next Step
I can't imagine it being that burdensome to accommodate variable STEPs. If it's going to be that inflexible, why have FOR ... NEXT ... STEP at all? Can anyone think of another language with this requirement? If I remember my C correctly:
Code: Select all
for (n = 0; n <= 10; n = n + 2)
Re: For Next Step
It is kind of like comparing apples to oranges, based on what the language author decides is "best".
In reality, the For loop in C does not have a Step at all! it is merely three consecutive statement groups, the middle "usually" a comparison. I say groups, because this is legal:
for (a=1, b=2, x=0; x<=10; x++, a++, b--) // note the "numerous" statements separated by commas.
Carrying that even further, in C, you can do just about any loop with just a For loop. You can omit any of the statements, and even this is legal:
for(;;) //infinite loop
In other syntax, C does not allow anything but constant integers in Switch (Select, in PB) statements. That is, this is illegal in C:
switch x {
case i : //fails because i is a variable
statements; break;
...but it is perfectly valid in PB. One reason is that most C compilers actually separate out the statements from the CASE tests, and put the statements after all of the CASE's so it is the equivalent of a string of "If Test is True Goto..." statements. To contrast that, PB merely parses it sequentially so there is a jump for each and every test.
And back you your original question: no, it would not be overly burdensome for variable steps to be implemented. However, many newbies would get confused by it so I will be surprised if Fred ever does it. Especially when there are other loops that are suitable for it.
In reality, the For loop in C does not have a Step at all! it is merely three consecutive statement groups, the middle "usually" a comparison. I say groups, because this is legal:
for (a=1, b=2, x=0; x<=10; x++, a++, b--) // note the "numerous" statements separated by commas.
Carrying that even further, in C, you can do just about any loop with just a For loop. You can omit any of the statements, and even this is legal:
for(;;) //infinite loop
In other syntax, C does not allow anything but constant integers in Switch (Select, in PB) statements. That is, this is illegal in C:
switch x {
case i : //fails because i is a variable
statements; break;
...but it is perfectly valid in PB. One reason is that most C compilers actually separate out the statements from the CASE tests, and put the statements after all of the CASE's so it is the equivalent of a string of "If Test is True Goto..." statements. To contrast that, PB merely parses it sequentially so there is a jump for each and every test.
And back you your original question: no, it would not be overly burdensome for variable steps to be implemented. However, many newbies would get confused by it so I will be surprised if Fred ever does it. Especially when there are other loops that are suitable for it.
Re: For Next Step
I think it would be more confusing for newbies to find that STEP must use a constant. Whatever.
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: For Next Step
I agree.I think it would be more confusing for newbies to find that STEP must use a constant.
No reason why there should not be two 'For Next' functions though, keep the current one and add an advanced version that accepts integer vars. Not all PB Newbies are new to programming, many already know at least one other language.
Last edited by IdeasVacuum on Sun Mar 18, 2012 3:40 am, edited 1 time in total.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
- BasicallyPure
- Enthusiast
- Posts: 539
- Joined: Thu Mar 24, 2011 12:40 am
- Location: Iowa, USA
Re: For Next Step
For what it's worth, this works.
BP
Code: Select all
inc = 2
For n = 0 To 10 Step 0
Debug n
n + inc
Next n
inc + 1
For n = 0 To 10 Step 0
Debug n
n + inc
Next n
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
Until you know everything you know nothing, all you have is what you believe.
Re: For Next Step
@chris319: I have two quotes from the developers that throw some light on this topic.chris319 wrote:What is the overarching reason for requiring that the target of STEP be a constant?
This quote was given Jan 27, 2004 in the thread http://www.purebasic.fr/english/viewtopic.php?f=3&t=928. If implemented this would have been accomplished in Version 3.90 .Fred wrote:Step (expression) is definitely a must have. I will try to implement it for the next version. Forget the DowTo stuff, I should be drunk
This quote was given May 11, 2009 in the thread http://www.purebasic.fr/english/viewtopic.php?f=3&t=37400.freak wrote:> 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.
It would seem from just these two quotes that Fred wanted to implement the idea at one point in time at least. It would also appear that when an expression is used for the step value a more complex condition is created for testing for the end of the loop.
Some discussion on aspects of the complexities can be found in the thread from 2009. It can be summed up by saying that with a variable start, stop, and step it would require a loop that tests for multiple situations in each iteration through the loop. If a constant is used then only one condition is tested for because it makes use of the sign of the step value at compile time.
I have a hope that it may still be implemented at some time in the future. In the meantime it can be accomplished with While/Wend or Repeat/Until loops.
Re: For Next Step
Not that anybody much cares, but there is an ANSI standard for BASIC which specifies that the target of STEP be a variable. The document is ANSI INCITS 113-1987 (R2003).
In Pascal it's worse. The increment is 1, unless you use the keyword DOWNTO instead of TO, in which case it is -1.
In Pascal it's worse. The increment is 1, unless you use the keyword DOWNTO instead of TO, in which case it is -1.
Re: For Next Step
That right there sums it up. There are no ANSI basic compilers, and very few come close.chris319 wrote:Not that anybody much cares, but there is an ANSI standard for BASIC...
Re: For Next Step
@BasicallyPure:
Unfortunately your code will not work when the variable inc is negative (say -2).
Even replacingbystill fails.
This complication is the main reason why I also think that the For..Next loop should natively support an expression for the Step.
Unfortunately your code will not work when the variable inc is negative (say -2).
Even replacing
Code: Select all
For n = 0 To 10 Step 0
Code: Select all
For n = 10 To 0 Step 0
This complication is the main reason why I also think that the For..Next loop should natively support an expression for the Step.
Anthony Jordan
Re: For Next Step
+1.1This complication is the main reason why I also think that the For..Next loop should natively support an expression for the Step.
Code: Select all
Vote.d=1.1
For x.d=0.1 To 10000 Step Vote
Debug x
Next
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Re: For Next Step
no not simple, it makes problems like this:
The reason is the inaccuracy of doubles
so, no For:To:Next with float or doubles!
Code: Select all
Define Integer.i
For Integer = 0 To 9 Step 3
Debug Integer/10.0 ; get 0.0, 0.3, 0.6 and 0.9
Next
Debug "---"
Define Double.d = 0.0
;For Double = 0.0 To 0.9 step 0.3
; Debug Double
;Next
While Double <= 0.9
Debug Double ; get only 0.0, 0.3 and 0.6 ... NOT 0.9
Double + 0.3
Wend
so, no For:To:Next with float or doubles!
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
Code: Select all
While ValD(StrD(Double,1)) <= 0.9
Debug StrD(Double,1) ; now it gets 0.9
Double + 0.3
Wend
Too many people think that floats are working like we learned at school (inlcuding me at the beginning). The forum is full of "is this a bug?" for that.... (please check....)
A way to prevent this ambiguity, is to fix the rounding automatically.
Another language uses PRECISION n for that.
Consider this:
Code: Select all
PRECISION 1; sets rounding automatically to 1, in human form
Define Double.d = 0.0
;For Double = 0.0 To 0.9 step 0.3
; Debug Double
;Next
While Double <= 0.9
Debug Double ; now it should get 0.9 too.
Double + 0.3
Wend
Numbers like 9.00000002 should be replaced with 9.00 and 8.999999997 also to 9.00
That way, farewell to the problems!
But it depends of Fred/Freak....
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Re: For Next Step
While that precision rounding could be handy, it significantly increases code complexity on the inside.
What might be another solution would be to have fixed-point numbers, rather than convoluted fixes for those who do not understand floating point. Fixed point are much faster--basically, they are integers, with a decimal point located n position from 0. For instance, fixed point of 2 could have a memory value of 12345, and would print on debug as 123.45.
In all reality, though, because of the naivete' of most newbie coders, integers could be used for almost everything. (Few understand that floats are insufficient for currency!) It would solve a lot of their problems, and make the code a lot more efficient.
What might be another solution would be to have fixed-point numbers, rather than convoluted fixes for those who do not understand floating point. Fixed point are much faster--basically, they are integers, with a decimal point located n position from 0. For instance, fixed point of 2 could have a memory value of 12345, and would print on debug as 123.45.
In all reality, though, because of the naivete' of most newbie coders, integers could be used for almost everything. (Few understand that floats are insufficient for currency!) It would solve a lot of their problems, and make the code a lot more efficient.