Game Development Community

Do-While Loop

by JeffH · in Torque 3D Beginner · 05/05/2013 (9:05 am) · 10 replies

Just wondering why there has never been a do-while loop in stock TorqueScript? Post test loops could be useful at times. This is just curiosity :P

- Jeff

#1
05/05/2013 (12:08 pm)
%I = someFunction(%stuff);
while (%I < %myTest)
{
    // do stuff with %I
    ...
    // then get next %I
    %I = someFunction(%nextStuff);
}
So, no "do while" but a "while." You have to "do" it first yourself.
#2
05/05/2013 (12:08 pm)
Whoops....
#3
05/05/2013 (7:07 pm)
There is?

$var = 10;
do
{
     echo($var);
     $var--;
}
while($var > 0)
#4
05/05/2013 (7:19 pm)
o_O I've tried something like that but I had no luck...weird there would be no semicolon on the while statement...oh the hidden wonders of Torque Script.

I'll try that later.

~Jeff
#5
05/05/2013 (7:28 pm)
No, there is not. This won't compile in T3D:
%count = 0;
	do
	{
		echo(" @@@ count: " @ %count);
		count++;
	}
	while (%count <= 10);

This, however, works:
%count = 0;
	while (%count <= 10)
	{
		echo(" @@@ count: " @ %count);
		%count++;
	}

Because... "do" is not a keyword in TorqueScript.
#6
05/05/2013 (7:58 pm)
@Richard yeah it seemed odd to me, was waiting to test until someone replied. Figured as much.

Thanks,

Jeff
#7
05/05/2013 (8:35 pm)
@Richard: My code snippet works perfectly fine in T3D (MIT version, but do-while's have been supported forever). In astNodes.cpp (~line 370) you can see where the command compiler does some special 'stuff' to support do-while loops.

Your snippet doesn't work for a couple reasons:

1). On line 5 you've missed a %. Code should be: %count++; not count++;
2). On line 7, you've added a semi-colon on the end of the while part which isn't valid TScript syntax.

The compiler enforces very strict formatting rules on do-while loops.

Here is a fixed version that works (I just tested it):
%count = 0;
do
{
	echo(" @@@ count: " @ %count);
	%count++;
}
while(%count <= 10)
#8
05/06/2013 (8:24 am)
I posted that before I corrected the missing % and the ;, but tested again after correcting each and it still failed each time. Perhaps I need to refresh my repository....

This is new functionality, hasn't been available for the past 10 years - I think this might have slipped in with the console refactor. Nice find!

(EDIT)
It appears this is in T2D as well - but Torsion doesn't pick up the "do" keyword. So it's been around longer than I thought - but it's undocumented, probably because of the extremely strict formatting or perhaps because someone added it without an assigned task <foreach, cough cough>.

Thanks Chris!

On another note, I've been using Torque (take your pick) for 10 years and never missed the "do."
#9
05/06/2013 (9:18 am)
I know do-while loops were supported in TGE 1.4.2 and I seem to recall using them once or twice before that. I don't remember if they were supported by v12/Tribes 2.

At any rate, I tend to avoid using them (even though they are typically faster than for or while loops) because the support for them is so... odd. The code in astNodes.cpp seems kind of hackish to me.
#10
05/06/2013 (1:34 pm)
Neat find!