For-Next Loops

BlitzMax Forums/BlitzMax Beginners Area/For-Next Loops

Mental Image(Posted 2006) [#1]
For x=0 To 10
	Print "x =" + x
Next

Print "Final x value is " + x


Why is x 11? Why does it get incremented after the loop? Does my head in, this............ :-)


Perturbatio(Posted 2006) [#2]
this works:
For x=0 Until 10
	Print "x =" + x
Next

Print "Final x value is " + x


(not entirely sure why To increments one further)

*EDIT*
the To appears to be doing the same as this:
Local x:Int
While x <= 10
	Print "x = " + x
	x:+1
Wend

Print "Final x value is " + x



Dreamora(Posted 2006) [#3]
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.


Mental Image(Posted 2006) [#4]
It's a pain in the neck.

If I have an array dimensioned to maxsize, I should be able to go:

for x=0 to maxsize
    array[x]=something
next


without getting an error because I am referencing outside the array. I have never encountered this in anything other than Blitz, but now I know I can work around it.


Perturbatio(Posted 2006) [#5]
local maxsize:int = 10
Local array:int[maxsize] 'create an array of 10 ints numbered 0-9
for x=0 to maxsize-1 'goes from 0-9
    array[x]=something
next



Diablo(Posted 2006) [#6]
local maxsize:int = 10
Local array:int[maxsize] 'create an array of 10 ints numbered 0-9
for x=0 to array.length-1 'goes from 0 to arrays length -1
    array[x]=something
next



Perturbatio(Posted 2006) [#7]
um... isn't that what I just said?


JazzieB(Posted 2006) [#8]
Use Until instead of To and you don't need to add '-1'...

Local maxsize:Int=10
Local array:Int[maxsize]
For x:Int=0 Until maxsize
    array[x]=something
Next

As for the value always being higher than what you put after To, it's always been like that in every BASIC since the dawn of time ;o)


Gabriel(Posted 2006) [#9]
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.


Perturbatio(Posted 2006) [#10]
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).


Diablo(Posted 2006) [#11]
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.


Yan(Posted 2006) [#12]
|oD

http://www.blitzbasic.com/Community/posts.php?topic=31922#341203


Mental Image(Posted 2006) [#13]
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.......


Mental Image(Posted 2006) [#14]
Indeed, Malcolm, and it shows that I still don't like it :-)


Gabriel(Posted 2006) [#15]
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.


Mental Image(Posted 2006) [#16]
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:

	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.


Dreamora(Posted 2006) [#17]
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)


Mental Image(Posted 2006) [#18]
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.