ForNext "Autostep"

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
MLK
User
User
Posts: 57
Joined: Sat Jan 24, 2004 8:46 pm
Location: Germany

ForNext "Autostep"

Post by MLK »

nothing happens:

Code: Select all

For i=10 To 0
    Debug i
Next
just with STEP -1

Code: Select all

For i=10 To 0 Step -1
    Debug i
Next
its ok so far, but it would be nice to have an option like:

Code: Select all

For i=10 To 0 Autostep 1
    Debug i
Next
or something else.
because often i use:

Code: Select all

for x=y to z
and would like to use the same code, even if z<y
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Mmmh! good quest!
It should be good 8)
Christian
Enthusiast
Enthusiast
Posts: 154
Joined: Mon Dec 08, 2003 7:50 pm
Location: Germany

Post by Christian »

I agree!

regards,
Christian
guido
User
User
Posts: 14
Joined: Tue Nov 25, 2003 4:56 pm
Location: Old Europe - Germany

Sign function would help, but...

Post by guido »

The ugly aspect of AUTOSTEP is, that it adds a keyword for a single scenario. (It wouldn't work at all in Pascal where TO a DOWNTO are distinct keywords.) Typically the problem is solved with the arithmetic sign function available as SGN in ancient BASICs.

Given that this function is missing in PB, it can easily be supplied

Code: Select all

Procedure.l Sgn(in.l)
result.l = 0
IF in <> 0:  result.l = in.l/ABS(in.l): EndIf
ProcedureReturn result
EndProcedure
So the generic for loop transforms to:

Code: Select all

For x=y To z Step(Sgn(z-y))
   Debug Str(i)
   Next
Unfortunately now the compiler complains, that only a numeric constant is allowed after Step. Perhaps that is the restriction, which should be removed? It wouldn't mean much of a change internally, if one defines, that the value of the Step expression is only evaluated once before entering the loop.
guido
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

i consider for / next to be something simple and quick to create loops, i'd go for while / wend for anything more complex
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
guido
User
User
Posts: 14
Joined: Tue Nov 25, 2003 4:56 pm
Location: Old Europe - Germany

Post by guido »

blueznl wrote:i consider for / next to be something simple and quick to create loops, i'd go for while / wend for anything more complex
In the extreme you could substitute all loops by if and goto :wink:. Striving for a code reflecting the problem it's out of question, that the purpose of a for loop is building a loop, where you know from the beginning, how often it is cycled and Repeat/Until While/Wend for those loops, where you don't. I can't recommend using the latter for the first scenario, even if it's possible...
guido
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

guido, i see your point

it's just a habit of mine to often use while / wends, as i often tend to mess with the variables / steps used for the loop inside the loop :-)

i started with a basic (exbasic) that would not allow modification of for / next parameters ie.

Code: Select all

' exbasic
for a = 1 to 10
  if whatever condition is met :-)
    a = 10
  endif
next a

would not exit the loop, so i very quickly developped a habit to go for

Code: Select all

a=0
while a<10
  a = a+1
  if whatever condition is met :-)
    a = 10
  endif
wend

i have no problem with the step parameter, but you are right, it would be better if it would take a variable, not just a constant (weird, never checked it or used it, moment... yep, that's something that could be improved, just like you i wouldn't care if it would only be evaluated during first execution of the for / next line)

so, although i don't care about autostep, i think i'd like the following two things added:

- Step <iexpr>
- Sgn( <iexp> )

i'll add them to my ever growing wishlist...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

A 'DownTo' keyword seems a cool solution, what do you think ?
User avatar
blueb
Addict
Addict
Posts: 1111
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Post by blueb »

If you're going to compare the 2 variables and STEP up or STEP down depending on their values then maybe AUTOSTEP is the better choice.
DOWNTO seems to infer that you'll always be STEPing down! :)

Then (if a user desires) they could use:

Code: Select all

For i=10 To 0 Autostep 1 
    Debug i 
Next
But still keep the existing STEP for compatiblity.

--blueb
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

Fred,

Introducing DOWNTO just worsens the problem, because you need separate keywords for either situation.

Imagine you are writing code to draw a line between two points, and you are using a for loop to step through.

The problem is that the points could be anywere on screen. p1 could be 100, 100 and p2 50, 50. With required an explicit step or a separate keywork you have to write extra code to see which value is the higher.

What you really want to do is to not have to worry which direction the step is in. if x1 is 100 and x2 is 50 then the compiler will understand that it needs to count down from 100 to 50.

Perhaps a TOWARDS key word? So when the code is

For a = X1 TOWARDS X2 STEP 2

The compiler will work out the necessary direction.

I think a new keyword would be necessary because some people may rely on the current behaviour.
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Post by GPI »

Fred wrote:A 'DownTo' keyword seems a cool solution, what do you think ?
No it isn't. It is a better solution to add the possibility to add a variable in the step-counter...
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

votes for: Step (expression)

And puts in a plug for SGN and NOT

:)
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

yes
yes

and yes

(and still people claim i'm a naysayer :-))
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: ForNext "Autostep"

Post by Danilo »

MLK wrote:nothing happens:

Code: Select all

For i=10 To 0
    Debug i
Next
its ok so far, but it would be nice to have an option like:

Code: Select all

For i=10 To 0 Autostep 1
    Debug i
Next
The compiler could automatically check whats the step count, 1 or -1.

Without Step, the compiler could compare FROM and TO:

Code: Select all

If FROM < TO
  STEP = 1
Elseif FROM > TO
  STEP = -1
; Else
;   STEP = 0 ; equal, no step needed
Endif
With Step given:

Code: Select all

If FROM < TO
  STEP = Step
Elseif FROM > TO
  STEP = -Step
; Else
;   STEP = 0 ; equal, no step needed
Endif
Would make things more easy for a BASIC language if its
checked automatically.

EDIT: Just forget it, sorry. Looks like i misunderstood the problem.
cya,
...Danilo
...:-=< http://codedan.net/work >=-:...
-= FaceBook.com/DaniloKrahn =-
guido
User
User
Posts: 14
Joined: Tue Nov 25, 2003 4:56 pm
Location: Old Europe - Germany

Post by guido »

@GPI, @GedB, @Dare2: Let me join the club! :D
Dare2 wrote:votes for: Step (expression)

And puts in a plug for SGN and NOT

:)
I couldn't have summarized it better. A new keyword just for blurring the sign of the step width seems overkill. TOWARDS is a very naturally sounding suggestion, but since AUTOSTEP is immediately preceeding the value it influences, AUTOSTEP wins the direct comparison. I consider DOWNTO the worst alternative for the reason given by GPI .

Just my 2 ct...
guido
Post Reply