ID:152971
 
I was wondering when I should use loops and which one should I use.
Use loops when you want something to happen multiple times, especially if it will happen a variable number of times.
for(var/index = 1 to 10)
src<<"Doing 10 jumping jacks... ([index])"

Deciding that you want a loop is simple. The more important part is choosing the correct type of loop. Loops in Byond include for, while, do-while and goto. You will probably get the most use out of for and while, and you might never use goto at all.

The for-loop is simple enough, but it has 3 different ways that it can be used. In each, the code block owned by the for-loop will execute, and a certain variable will be changed each time depending on how it is looping.

The first is as in the above code example with the jumping jacks. It simply executes with the variable incrementing from the first number to the second number.
mob/proc/MyProc()
var/N
for(N = 1 to 3)
src<<N
//will execute 3 times, outputing 1, then 2, then 3

The next version I will describe is similar, but it is used to loop through the items of a list. You use "variable in list", and the loop will go through list.len times with the variable being equal to an item in the list each time.
mob/proc/MyProc()
var/list/L=list("a","b","c")
var/item
for(item in L)
src<<item
//will execute 2 times, outputting a, then b, then c

The last version is somewhat more complicated. This version has 3 parameters, called the initialization, the test and the increment. It has four steps.

(1)It first does whatever the initialization tells it to do.

(2)Then it checks to see if the test is true (just like in an if statement).

(3)Then it will execute the for-loop's code block if test is true.

(4)After that, it does the increment part, which is usually to do a bit of math on a variable.

It then goes back to step 2. Therefor, it repeats until test results in false.
mob/proc/MyProc()
var/N
for(N=1, N<=3, N+=1)
src<<N
//will execute 3 times, outputting 1, 2 then 3

To make that into a form of easier to understand pseudocode...
mob/proc/MyProc()
var/N
for(set N to 1, execute as long as N<=3, add 1 to N then test again)

As for the while loop, that is fairly simple to understand. while has one parameter, and that is a testing one. As long as it results in true, it executes its code.
mob/proc/MyProc()
var/N=1
while(N<=3)
src<<N
N+=1
//will execute 3 times

Be extra careful with while loops, as they are easy to make a mistake with which can result in an infinite loop, freezing the program.

The do-while version of the while loop is only slightly different. You put the while part after the code block, and the keyword "do" before it. The only difference is that it is sure to execute at least once.
mob/proc/MyProc()
var/N=1
do
src<<N
N+=1
while(N<=3)

The usefulness in that comes in situations like the following.
mob/var/title
mob/Login()
do
title=input(src,"","What is your title?")
while(!title)
..()

In that, if you used a normal while loop it would do nothing, as title starts as null and would return false in the while. However, using do-while, it is forced to execute at least once, letting the player input his/her title first before it gets checked to make sure they input something.

As for goto, you just make a label in the code and then goto tells it to return to the label. This can be used for more than just looping; however, you should not be in need of it unless you have some tangled, complex web of a set of loops, which is rarely the case.