Page 1 of 2

ForNext "Autostep"

Posted: Sat Jan 24, 2004 9:02 pm
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

Posted: Sun Jan 25, 2004 1:14 am
by Psychophanta
Mmmh! good quest!
It should be good 8)

Posted: Sun Jan 25, 2004 12:04 pm
by Christian
I agree!

regards,
Christian

Sign function would help, but...

Posted: Mon Jan 26, 2004 9:12 am
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.

Posted: Mon Jan 26, 2004 9:48 am
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

Posted: Mon Jan 26, 2004 11:16 am
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...

Posted: Mon Jan 26, 2004 12:17 pm
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...

Posted: Mon Jan 26, 2004 3:11 pm
by Fred
A 'DownTo' keyword seems a cool solution, what do you think ?

Posted: Mon Jan 26, 2004 3:47 pm
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

Posted: Mon Jan 26, 2004 5:31 pm
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.

Posted: Mon Jan 26, 2004 6:44 pm
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...

Posted: Tue Jan 27, 2004 12:18 am
by Dare2
votes for: Step (expression)

And puts in a plug for SGN and NOT

:)

Posted: Tue Jan 27, 2004 1:20 am
by blueznl
yes
yes

and yes

(and still people claim i'm a naysayer :-))

Re: ForNext "Autostep"

Posted: Tue Jan 27, 2004 8:35 am
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.

Posted: Tue Jan 27, 2004 9:25 am
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...