For Next Step

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: For Next Step

Post by chris319 »

What is the overarching reason for requiring that the target of STEP be a constant?
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: For Next Step

Post by Tenaja »

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.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: For Next Step

Post by chris319 »

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)
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: For Next Step

Post by Tenaja »

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.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: For Next Step

Post by chris319 »

I think it would be more confusing for newbies to find that STEP must use a constant. Whatever.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: For Next Step

Post by IdeasVacuum »

I think it would be more confusing for newbies to find that STEP must use a constant.
I agree.

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.
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: For Next Step

Post by BasicallyPure »

For what it's worth, this works.

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
BP
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: For Next Step

Post by Demivec »

chris319 wrote:What is the overarching reason for requiring that the target of STEP be a constant?
@chris319: I have two quotes from the developers that throw some light on this topic.
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 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 .

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.
This quote was given May 11, 2009 in the thread http://www.purebasic.fr/english/viewtopic.php?f=3&t=37400.


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.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: For Next Step

Post by chris319 »

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.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: For Next Step

Post by Tenaja »

chris319 wrote:Not that anybody much cares, but there is an ANSI standard for BASIC...
That right there sums it up. There are no ANSI basic compilers, and very few come close.
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Re: For Next Step

Post by akj »

@BasicallyPure:

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
by

Code: Select all

For n = 10 To 0 Step 0
still fails.

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
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: For Next Step

Post by charvista »

This complication is the main reason why I also think that the For..Next loop should natively support an expression for the Step.
+1.1

Code: Select all

Vote.d=1.1
For x.d=0.1 To 10000 Step Vote
    Debug x
Next
This does not work (yet). But how simple.
- 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%
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: For Next Step

Post by STARGÅTE »

no not simple, it makes problems like this:

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
The reason is the inaccuracy of doubles

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 moreTypeface - Sprite-based font include/module
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: For Next Step

Post by charvista »

Code: Select all

    While ValD(StrD(Double,1)) <= 0.9
       Debug StrD(Double,1) ; now it gets 0.9
       Double + 0.3
    Wend
Needs to be rounded first, of course.
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
PRECISION 2 should be the default value, as most currencies have 2 digits after the decimal point.
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%
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: For Next Step

Post by Tenaja »

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.
Post Reply