ID:168522
 
What is to, what is it for, how does it work? It seems to be very useful, but I haven't a clue on how to use it. I would've searched the forum, but does anyone know how common the word to is? I'd get thousands of results.
At this moment, I can think of two examples you can use the "to" operator with. One using for(), and the other using switch().

For example, instead of a more complicated for loop:
for(var/j=1,j<=10,j++)
src << j


We can just use the "to" operator to go by the numbers automatically.

for(var/j = 1 to 10)
src << j


They both end up doing the same thing. The first one is usually used for more complicated loops.

Mind that you can also do this to loop 10 times, but it is only useful to loop something 10 times without knowing what number it is at.

for(1 to 10)
src << "Loop!"


The second way I can think of right now is using it with switch(). You can check if a number is in a range of two numbers.

var/x = rand(1, 10)
switch(x)
if(1) src << "It is 1"
if(2 to 5) src << "It is in a range of 2 to 5"


I might have missed some ways to use to, so there might be more.

It's odd that the BYOND Forums highlight "to" in the dm tags, but not the DM compiler.

~~> Dragon Lord
In response to Unknown Person
Unknown Person wrote:

> var/x = rand(1, 10)
> switch(x)
> if(1) src << "It is 1"
> if(2 to 5) src << "It is in a range of 2 to 5"
>

I've never seen that way in use. I might try it sometime :)
In response to Unknown Person
Unknown Person wrote:

> for(var/j = 1 to 10)
> src << j
>



You can also use step in that loop.

for(var/i = 2 to 100 step 2)
src << i
In response to Ben G
Theres a step function o.O'? I need to re-read the dm guide :o.
In response to Xeal
It's one of those undocumented things.
In response to Nadrew
step?? whats its function?

Blu
In response to Xeal
The following three lines of code are equivalent, and all output the same thing:

for(var/i = 0 to 100) world << i
for(var/j = 0 to 100 step 1)
for(var/k = 0,k <= 100, k++) world << k

/*Outputs:
0
1
2
3
4
5
...
100


When you write "for(var/i = 1 to 100)," the "step 1" part is implied. The number after "step" tells BYOND how much to add to the variable per iteration. Therefore, the following two lines are equivalent, and both output the same thing:

for(var/i = 0 to 100 step 2)
for(var/j = 0,j <= 100, j += 2)

/*Outputs:
0
2
4
6
8
...
100*/


Note that step also works with negative numbers:

for(var/i = 100 to 0 step -1)

/*Outputs:
100
99
98
97
96
...
0*/
In response to Nadrew
So eh, why do we have so many undocumented things? It would be so much easier to know stuff like this(I knew the to thing. I had never heard of step before, but it was an easy guess as to what it did.).

Is 4.0's Reference going to have a fuller documentation?

Hiead