While Wend & For Next Query

Monkey Forums/Monkey Beginners/While Wend & For Next Query

Drex(Posted 2014) [#1]
GLFW & Android work fine with this :

loop = 0

While loop < 1050

xpos [loop]= Rnd (1,1680)

loop = loop + 1

Wend



GLFW likes this, but Android doesn't.....why ?

For loop = 0 to 1050

xpos [loop]= Rnd (1,1680)

Next


Gerry Quinn(Posted 2014) [#2]
You haven't told us why you think they don't like it. At a wild guess, I think you're getting a runtime crash because xpos has only 1050 elements. Java will complain when you try to write past the end of the array, whereas C++ will happily accept that the programmer knows best.

You should use For..Until rather than For...To in most circumstances.


Drex(Posted 2014) [#3]
You are correct, I was trying to write past the end of the array :(

Thanks for the tips...


Goodlookinguy(Posted 2014) [#4]
For future quick references for anyone else confused by this...

'To' is '<=' (less than equal)
'Until' is '<' (less than)

So if you find yourself ever having to write '... To someValue - 1' just write '... Until someValue'. More often than not, 'Until' is the correct keyword. For some reason though, the 'To' keyword has become incredibly prevalent in posts on the forums and causes a lot of confusion.


Gerry Quinn(Posted 2014) [#5]
The way I look at it is that "Until" is pretty much always the one you need when you are interested in a particular *number* of things (which includes "all the things" in an array etc.).

"To" can come in handy when you are looking for a *range* of things. For example, if you want the eight squares surrounding a particular square on a 2D grid, you might write:

For Local dx:Int = -1 To 1
	For Local dy:Int = -1 To 1
		If dx <> 0 Or dy <> 0
			DoSomething( x + dx, y + dy )
		End
	Next
Next



Nobuyuki(Posted 2014) [#6]
more info: "Until" is a piece of syntactic sugar (which I've never seen outside of Monkey!) that allows the code to loop through from the startvalue (inclusive) to the endvalue (exclusive). It stops before executing the code block where the iterator is endvalue. This is mostly useful when you want to loop an iterator through the entire length of an array or container with a 0-based index, because you can specify the container's length using its Length property as the endvalue and can arguably increase code readability.

There are cases where that readability falls apart, and in those cases you should of course fall back to the standard For loop syntax


Goodlookinguy(Posted 2014) [#7]
Hmm? More often than not "standard" For loop syntax would be with the 'Until' keyword. People just use the wrong one a lot of the time, plain and simple.

When you write code in C-based languages, more often than not you write...
for ( i = 0; i < someValue; i++ )

which is like using 'Until'. Not...
for ( i = 0; i <= someValue - 1; i ++ )

which is like using 'To'.

Oh, and the 'Until' keyword is in BlitzMax which is sort-of the predecessor to Monkey here. Although it's only used in the 'repeat...until' type of loop in that language since BlitzMax uses a 1-based index system.