Jump to content

My first script with a loop - Why isn't it working?


daveh1973
 Share

You are about to reply to a thread that has been inactive for 4375 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Hi. Can anyone tell me why my code gets a syntax error at the Do of the Do...While loop?

float wsquared(float w)   // return the square of the argument
{
    return w*w;
}
float thimblea=106.039;
float thimbleb=200.718;
string txt="";
float OldDist=900;
float newDist=900;
default
{
    attach(key id)
    {
        vector avpos=llGetPos();
        float posx=avpos.x;
        float posy=avpos.y;
        float oldDist=llSqrt(wsquared(llAbs(thimblea-posx))+wsquared(llAbs(thimbleb-posy))); //use pythagoras to work out the distance
        
        integer a=0;
        integer b=1000;
        Do
    {
            a++;
            string txt=" ";
        }while(a<b);
           
           
        }
}       

Link to comment
Share on other sites

There is nothing wrong with do-while() loops just as there is nothing wrong with jump instruction. And yet elegant code tries to avoid either. This applies not only to LSL but to all C-like-syntax languages. There is no loop executed by do-while() that cannot be done (and usually more efficiently) by for() or while()

So my advice to you as an admitted beginner is to avoid this construct.

Link to comment
Share on other sites


Ela Talaj wrote:

So my advice to you as an admitted beginner is to avoid this construct.


A do-while loop runs through the loop body then checks condition, so we have at least one run.

A while loop checks condition 1st then runs through the loop body, so it's possible that we have no run.

A for loop can replace both loops and vise versa.

There is absolutely no reason to avoid a do-while loop. Just choose the right loop for your code.

There is always one loop that matches best and the do-while is the best choice if you need at least one run through the loop body.

The thread starters example doesn't contain anything useful so you can't say if it's good or bad. :)

Link to comment
Share on other sites

In fact, I've found that if there's any LSL loop construct to be cautious about using, it's the for loop, but only for performance reasons.

(Personally, I too eschew jump unless there's a dire need for it, but YMMV. I just find my code quicker to read if I can safely assume there won't be any jumps.)

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 4375 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...