Author Topic: Do/Loop Until  (Read 842 times)

Is this possible in Blockland? I can't figure out the syntax.

I looked all over for this in a previous project of mine. All that I could find was While() .

It's basically the same format as a for loop.  Unless it would have any special functionality in torque, there isn't much need for it other than syntax.

It's basically the same format as a for loop.  Unless it would have any special functionality in torque, there isn't much need for it other than syntax.

A Do/Loop Until will loop until the test case is true, and a Do/While will loop while the test case is true. This means a Do/Loop Until checks the test case after the loop has been executed, and a while does it before. There is a difference.

A Do/Loop Until will loop until the test case is true, and a Do/While will loop while the test case is true. This means a Do/Loop Until checks the test case after the loop has been executed, and a while does it before. There is a difference.
I always thought them the same, thanks for clarification.
One thing though: What a Do/Loop Until does can be done in other loops, so is it mainly for convenience in coding or is it more efficient to use?

For convenience and readability (which is very important in programming).

I always thought them the same, thanks for clarification.
One thing though: What a Do/Loop Until does can be done in other loops, so is it mainly for convenience in coding or is it more efficient to use?
My Raycasting Weapons script currently uses a 'while' loop and I have to duplicate code. I was looking to reduce the size of this for a similar use for raycasts. The basic flow of the code is like this:
Code: [Select]
if(range > 100)
{
   stored range = range - 100
   range = 100
}
do a raycast
if(hit something)
{
   do hit code
   if(weapon pierces targets)
      stored range += distance to target
   else
      stored range = 0
}
while(stored range > 0)
{
   if(range > 100)
   {
      stored range = range - 100
      range = 100
   }
   do a raycast
   if(hit something)
   {
      do hit code
      if(weapon pierces targets)
         stored range += distance to target
      else
         stored range = 0
   }
}
If I had a 'do/loop until' construct in Torque, I could shorten it to this.
Code: [Select]
do
{
   if(range > 100)
   {
      stored range = range - 100
      range = 100
   }
   do a raycast
   if(hit something)
   {
      do hit code
      if(weapon pierces targets)
         stored range += distance to target
      else
         stored range = 0
   }
}
loop until (stored range <= 0)

Code: [Select]
do
{
   if(range > 100)
   {
      stored range = range - 100
      range = 100
   }
   do a raycast
   if(hit something)
   {
      do hit code
      if(weapon pierces targets)
         stored range += distance to target
      else
         stored range = 0
   }
}
loop until (stored range <= 0)

Wouldn't a while loop, like such, do the same thing?

Code: [Select]
while(1)
{
   if(range > 100)
   {
      stored range = range - 100
      range = 100
   }
   do a raycast
   if(hit something)
   {
      do hit code
      if(weapon pierces targets)
         stored range += distance to target
      else
         stored range = 0
   }

   if(stored range <= 0)
      break
}

(Obviously not proper torque syntax; just re-arranging your code.)