# PureBasic Forum

 It is currently Sat May 25, 2013 12:28 am

 All times are UTC + 1 hour

 Page 2 of 3 [ 34 posts ] Go to page Previous  1, 2, 3  Next
 Print view Previous topic | Next topic
Author Message
 Post subject: Re: For Next StepPosted: Sat Mar 17, 2012 1:39 pm
 Enthusiast

Joined: Mon Oct 24, 2005 1:05 pm
Posts: 386
What is the overarching reason for requiring that the target of STEP be a constant?

Top

 Post subject: Re: For Next StepPosted: Sat Mar 17, 2012 5:53 pm
 Enthusiast

Joined: Tue Nov 09, 2010 10:15 pm
Posts: 794
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.

Top

 Post subject: Re: For Next StepPosted: Sat Mar 17, 2012 9:56 pm
 Enthusiast

Joined: Mon Oct 24, 2005 1:05 pm
Posts: 386
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:
for (n = 0; n <= 10; n = n + 2)

Top

 Post subject: Re: For Next StepPosted: Sat Mar 17, 2012 11:02 pm
 Enthusiast

Joined: Tue Nov 09, 2010 10:15 pm
Posts: 794
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.

Top

 Post subject: Re: For Next StepPosted: Sun Mar 18, 2012 12:56 am
 Enthusiast

Joined: Mon Oct 24, 2005 1:05 pm
Posts: 386
I think it would be more confusing for newbies to find that STEP must use a constant. Whatever.

Top

 Post subject: Re: For Next StepPosted: Sun Mar 18, 2012 3:35 am

Joined: Fri Oct 23, 2009 2:33 am
Posts: 2863
Location: Wales, UK
Quote:
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.

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.

Last edited by IdeasVacuum on Sun Mar 18, 2012 3:40 am, edited 1 time in total.

Top

 Post subject: Re: For Next StepPosted: Sun Mar 18, 2012 3:40 am
 Enthusiast

Joined: Thu Mar 24, 2011 12:40 am
Posts: 155
Location: Iowa, USA
For what it's worth, this works.
Code:
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

Top

 Post subject: Re: For Next StepPosted: Sun Mar 18, 2012 6:09 am

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 2401
Location: Utah, USA
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 [url]http://www.purebasic.fr/english/viewtopic.php?f=3&t=928[/url]. 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 [url]http://www.purebasic.fr/english/viewtopic.php?f=3&t=37400[/url].

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.

_________________

Top

 Post subject: Re: For Next StepPosted: Sun Mar 18, 2012 6:25 pm
 Enthusiast

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

Top

 Post subject: Re: For Next StepPosted: Sun Mar 18, 2012 11:21 pm
 Enthusiast

Joined: Tue Nov 09, 2010 10:15 pm
Posts: 794
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.

Top

 Post subject: Re: For Next StepPosted: Fri Mar 23, 2012 12:51 pm
 Enthusiast

Joined: Mon Jun 09, 2003 10:08 pm
Posts: 635
Location: Nottingham
@BasicallyPure:

Unfortunately your code will not work when the variable inc is negative (say -2).
Even replacing
Code:
For n = 0 To 10 Step 0
by
Code:
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

Top

 Post subject: Re: For Next StepPosted: Fri Mar 23, 2012 9:47 pm
 Enthusiast

Joined: Tue Sep 23, 2008 11:38 pm
Posts: 702
Location: Belgium (& Luxembourg)
Quote:
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:
Vote.d=1.1
For x.d=0.1 To 10000 Step Vote
Debug x
Next

This does not work (yet). But how simple.

_________________
- Future conversation forecasting not yet implemented.
- If the future had copied a program from now, they would have called it version -1.

Top

 Post subject: Re: For Next StepPosted: Fri Mar 23, 2012 10:17 pm
 Enthusiast

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 711
Location: Germany, Glienicke
no not simple, it makes problems like this:

Code:
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!

_________________

Top

 Post subject: Re: For Next StepPosted: Fri Mar 23, 2012 10:51 pm
 Enthusiast

Joined: Tue Sep 23, 2008 11:38 pm
Posts: 702
Location: Belgium (& Luxembourg)
Code:
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:
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....

_________________
- Future conversation forecasting not yet implemented.
- If the future had copied a program from now, they would have called it version -1.

Top

 Post subject: Re: For Next StepPosted: Fri Mar 23, 2012 11:35 pm
 Enthusiast

Joined: Tue Nov 09, 2010 10:15 pm
Posts: 794
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.

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 2 of 3 [ 34 posts ] Go to page Previous  1, 2, 3  Next

 All times are UTC + 1 hour

#### Who is online

Users browsing this forum: No registered users and 2 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite