ID:265910
 
I was looking at the do proc in the DM reference and was wondering which way would be better o.O
var/i = 3
do
world<<i--
while(i)

//or

var/i = 3
while(i)
world<<i
i--

//or

for(var/i=3,i>0,i--)
world<<i
There is a difference between do/while and while. In do/while, the condition is checked after the first iteration, so it always occurs at least once.
In response to Kaiochao
I guess I'd have to give a scenario for you guys to give a definite answer then...

Hmm... How about for a basic countdown? Just like the examples given xD
In response to Spunky_Girl
Obviously you'd use for. You use for when you know how many iterations there are going to be, while the other two are used when you know what the conditions are either to keep going or to stop the loop.

And, once again, more evidence that you shouldn't be helping anyone. This is really basic stuff.
Just as a note though, I'm fairly sure they all end up the same thing in the compiled code anyways. Loops are dumbed down pretty far when they're compiled in.

They end up similar to:
somelabel:
if(something <= something_else)
// Do stuff
goto somelabel


Not sure if 'do while' and 'do' both equate to the same, but I know 'while' and that syntax of 'for' do.
In response to Nadrew
Exactly, and so it doesn't really matter which one you opt to use in cases like this.
In response to Kaioken
Could you give me examples for each one where it should be used over the others? To elaborate, give an example where 'do while' is preferable over 'while' or 'for', and then the same for the others.
In response to Spunky_Girl
May I reference the guide here?
I'd say that Chapter 6.17 offers a nice description.

For example:

This (while) loop is mainly useful in situations where the initialization and iteration statements are unnecessary or can be combined with the condition. For example, the for loop example can be made to work with a simple while loop.

obj/scroll/medicine
verb/cast(N as num)
while(N-- > 0)
new/obj/medicine(usr)


The do while loop is similar to the while loop, except the condition is tested at the end of an iteration rather than the beginning. The effect this has is to guarantee that the body of the loop is executed at least once. In certain situations, that is just what one needs.
For example, one could make the medicine verb work without any argument at all (when the player is in a hurry for some medicine).

obj/scroll/medicine
verb/cast(N as num|null)
do
new/obj/medicine(usr)
while(--N > 0)
In response to Spunky_Girl
Whichever should be used is whichever you find most convenient to use (for the situation at hand). Both for() and while() are similar and very often 2 available methods of doing the exact same thing (the only difference is how the code looks). Same thing can be said about using goto directly.
do-while() on the other hand is obviously different from the other loop constructs in that it'll execute the block under it once unconditionally and only check the condition for subsequent iterations. So it's basically a shortcut instead of duplicating the statement again before the loop which is used for running the statement at least once, as with other loops it could potentially not run at all.
do
Statement
while(X)
//same as
Statement
while(X)
Statement

A couple of the common uses could be like:
mob/verb/change_name()
do
var/X = input("Choose your name.")
while(!X) //keep asking again until a non-empty string is given
//...

proc/find_last(maintxt,needle)
var/find,pos
do
pos = find
find = findtext(maintxt,needle,pos+1)
while(find)
return pos

<small>EDIT: Fixed formatting issue.</small>
In response to Nadrew
do/while is very similar:
label:
// do stuff
if(condition) goto label
// rest of code


Though there is a special opcode to handle the if-goto construction in a single bytecode, rather than using the standard 'if' and 'goto' bytecodes which are used for while() and for() loops.