For-Next Loops
BlitzMax Forums/BlitzMax Beginners Area/For-Next Loops Incrementation border is defined as "as long as x smaller-equal "to value"" (while x<=10) which means the same as "while x between 0 and 10 to execute the code within the For - Next block".
This is why it comes out one step larger than the to, which is the first x that step outside the allowed "max" of 10.
This is why it comes out one step larger than the to, which is the first x that step outside the allowed "max" of 10.
um... isn't that what I just said?
It's a pain in the neck.
Your issues are completely unrelated. Your referencing outside the array because arrays stretch from 0 to size-1, as Perturbatio demonstrated.
I'd love to know what languages you've used that don't do this because every basic variant I've ever used sure as hell does. PureBasic does, IBasic does. It has absolutely no effect on the loop contents whatsoever because the counter is not incremented until the Next command is issued, by which time your loop is complete.
I can actually see a use for this functionality as well.
if you were iterating through a collection of values and processing them, but wanted to do something different with the last one.
(i.e. you were appending a "," to each string in an array of strings but didn't want the last string to have one).
if you were iterating through a collection of values and processing them, but wanted to do something different with the last one.
(i.e. you were appending a "," to each string in an array of strings but didn't want the last string to have one).
um... isn't that what I just said?
the method i used just means the array can be any size i.e once hes created the array he can re-size it and it will still work.
My issues are *NOT* unrelated. I know perfectly well how to reference arrays, and used that as a quick example that didn't really show what I meant.
My point was, and is, that in a loop of x=0 to 10, I would not expect x to ever equal 11, which it does in Blitz when using for x=? TO.. I don't recall ever seeing that in any of the many languages I have programmed in over the years.
The relevance to the arrays is completely misleading, and was just a bad example by me.
Thanks guys, anyway, I shall be using for-until from now on.......
My point was, and is, that in a loop of x=0 to 10, I would not expect x to ever equal 11, which it does in Blitz when using for x=? TO.. I don't recall ever seeing that in any of the many languages I have programmed in over the years.
The relevance to the arrays is completely misleading, and was just a bad example by me.
Thanks guys, anyway, I shall be using for-until from now on.......
Indeed, Malcolm, and it shows that I still don't like it :-)
My issues are *NOT* unrelated. I know perfectly well how to reference arrays, and used that as a quick example that didn't really show what I meant.
Then I'm confused as to why you wrote :
I should be able to go: .. without getting an error because I am referencing outside the array.
Because this behaviour can never cause a loop to go out of bounds in an array within it.
My point was, and is, that in a loop of x=0 to 10, I would not expect x to ever equal 11
And my point is that since it never equals 11 inside the loop, it never matters. Do you plan to go on and do something with x, expecting it to still be 10? If so, I can't read minds, because you don't do that in any of your examples, nor did you describe any intention to so do.
Because, as I have already said, I used a bad example. What I was actually trying to do was read in some data and then set an end marker as follows:
So, basically I was reading in the data from the file, and then manually writing the end marker. Because I expected x to be the maximum value I had set in the loop, it seemed reasonable to write the two markers to x+1 and x+2, but the latter was outside the bounds of my array.
I accept that you can't read minds, and my example was bad, but this is why I have a problem with the loop increment, and how it is relative to an array. I had forgotten that I had the same problem with BlitzPlus.
Thanks for your help.
alien_path=alien_path[..ALIENPOINTS+1] 'ALIENPOINTS is the maximum number of points 'array dimensioned to two above max for endmarkers file=OpenStream(FileName$) If Not file RuntimeError "failed to open alien data file" For x=0 To ALIENPOINTS-1 alien_path[x]=ReadInt(File) 'read the path data Next alien_path[x+1]=ENDMARKER 'write the two end markers alien_path[x+2]=ENDMARKER
So, basically I was reading in the data from the file, and then manually writing the end marker. Because I expected x to be the maximum value I had set in the loop, it seemed reasonable to write the two markers to x+1 and x+2, but the latter was outside the bounds of my array.
I accept that you can't read minds, and my example was bad, but this is why I have a problem with the loop increment, and how it is relative to an array. I had forgotten that I had the same problem with BlitzPlus.
Thanks for your help.
Blitz3D or better all old Blitz created an array of 0 to maxsize when you initialized them with size maxsize ... its about the only language that has this wrong behavior (c derivates have 0 to maxsize - 1, basics/pascal derivates have 1 to maxsize normally)
I didn't know that about 3D. But that wasn't really the issue as explained in my post above. My array was dimensioned to ALIENPOINTS + 1 which, in effect, is ALIENPOINTS -1 (which is the standard maxsize -1) plus another two slots for the end markers.
The problem arose because of the x increment outside the loop. In fact this whole thread would have been sorted if I had just posted the code in the beginning and not sent everyone off on a wild goose chase!
So, thanks to all for the replies, and the help, now I know what it does, I will work around it in future similar situations.
The problem arose because of the x increment outside the loop. In fact this whole thread would have been sorted if I had just posted the code in the beginning and not sent everyone off on a wild goose chase!
So, thanks to all for the replies, and the help, now I know what it does, I will work around it in future similar situations.