The For( Command / discussion
Started by: Automatic
On: 1198164307|%e %b %Y, %H:%M %Z|agohover
Number of posts: 8
rss icon RSS: New posts
This is the discussion related to the wiki page The For( Command.
Closing ) Optimization
DarkerLineDarkerLine 1198164567|%e %b %Y, %H:%M %Z|agohover

Have you actually done tests to confirm this, and if so, what did you test? I ran a test of my own and got a slight difference in favor of no closing parenthesis.

With a closing parenthesis: 20 bars 3 pixels.

:For(I,1,4000)
:If 1:
:End

Without a closing parenthesis: 19 bars 6 pixels
:For(I,1,4000
:If 1:
:End
Reply  |  Options
Unfold Closing ) Optimization by DarkerLineDarkerLine, 1198164567|%e %b %Y, %H:%M %Z|agohover
Re: Closing ) Optimization
DarkerLineDarkerLine 1198165204|%e %b %Y, %H:%M %Z|agohover

Hm… I do get a striking difference for DS>(:

With a closing parenthesis: 4 bars 3 pixels.

:For(I,1,500)
:DS<(J,0
:End

Without a closing parenthesis: 14 bars 6 pixels.

:For(I,1,500
:DS<(J,0
:End

This was tested when J was always >0, but if you change it to <0 and add an extra empty line to skip, nothing changes.

Reply  |  Options
Unfold Re: Closing ) Optimization by DarkerLineDarkerLine, 1198165204|%e %b %Y, %H:%M %Z|agohover
Re: Closing ) Optimization
DarkerLineDarkerLine 1198165423|%e %b %Y, %H:%M %Z|agohover

Finally, there is a difference for If statements but only when the condition is negative —

With a closing parenthesis: 2 bars 4 pixels.

:For(I,1,500)
:If 0:
:End

Without a closing parenthesis: 13 bars 1 pixel.

:For(I,1,500
:If 0:
:End

I'll go back in the article to clarify things. I think this might shed light on a few other details as well.

Reply  |  Options
Unfold Re: Closing ) Optimization by DarkerLineDarkerLine, 1198165423|%e %b %Y, %H:%M %Z|agohover
Re: Closing ) Optimization
burrburr 1198169368|%e %b %Y, %H:%M %Z|agohover

No, I didn't do any tests myself; rather I used this post by Weregoose.

Reply  |  Options
Unfold Re: Closing ) Optimization by burrburr, 1198169368|%e %b %Y, %H:%M %Z|agohover
For loop behavior
Edward HEdward H 1259128977|%e %b %Y, %H:%M %Z|agohover

The description of the exact steps of the For( loop was horrendously wrong. Before it said the behavior of For(var,start,end[,step]) was:

  1. Set var to equal start.
  2. Run all the statement(s).
  3. Increase the value of var by step (or by 1, if you didn't provide a step)
  4. As long as var is no greater than end (or no less than, if step is negative), go back to step 2.

It's actually:

  1. Set var to equal start.
  2. If var is greater than end (or less than, if step is negative), quit the For loop immediately.
  3. Run the statements.
  4. Add step to var.

Thus, we can have For( loops that never run the statements, and also, the variable usually ends up being end + step after the loop is over. I felt the need to point this out because, considering the For( loop is one of the most fundamental parts of BASIC, this is a big deal.

Example: if you run For(A,B,0:End, with B>0, then what you've just done is set B to A without changing Ans. This could be useful in many situations.

Example 2: You can use the loop counter after the loop ends for useful things. In order to do this, you need to know what the loop counter should equal after you leave the loop.

Perhaps we should document the behavior of the loop counter after the loop closes.

Last edited on 1259130099|%e %b %Y, %H:%M %Z|agohover By Edward H + Show more
Reply  |  Options
Unfold For loop behavior by Edward HEdward H, 1259128977|%e %b %Y, %H:%M %Z|agohover
Re: For loop behavior
thornahawkthornahawk 1259132683|%e %b %Y, %H:%M %Z|agohover

As it looks, the value of var after For( loop execution is start+step*⌊1+(end-start)/step

thornahawk

Last edited on 1259133349|%e %b %Y, %H:%M %Z|agohover By thornahawk + Show more
Reply  |  Options
Unfold Re: For loop behavior by thornahawkthornahawk, 1259132683|%e %b %Y, %H:%M %Z|agohover
Re: For loop behavior
Edward HEdward H 1259195323|%e %b %Y, %H:%M %Z|agohover

Good call. Although that's only if end * step > start * step.

I guess it should be start+step*max(0,⌊1+(end-start)/step⌋)
…but that looks kind of messy.

Reply  |  Options
Unfold Re: For loop behavior by Edward HEdward H, 1259195323|%e %b %Y, %H:%M %Z|agohover
Re: For loop behavior
thornahawkthornahawk 1259199258|%e %b %Y, %H:%M %Z|agohover

I should've added "if the For( loop executes at all" in my post. :D

"var gets assigned the value of start if start>end and step>0, or start<end and step<0"

compared to

"var gets assigned the value of start if start*step>end*step"

It's very nice how you easily conflated the two inequalities. ;)

thornahawk

Reply  |  Options
Unfold Re: For loop behavior by thornahawkthornahawk, 1259199258|%e %b %Y, %H:%M %Z|agohover
New Post