Jump to content

Incrementing in Do While Flow control


Gadox
 Share

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

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

Recommended Posts

Hello, I have been scratching my head, trying and looking around without finding anything that can help me.

I try to make the loop Do While increment by 2 each loop but starting at 0 for the first result then 2,4,6,8...etc.

This increment by 1 from 0 to 10

//integer x;
default
{
    touch_start(integer total_number)
    {
        integer x;
        do
            llSay(0, (string)x);
            //x = x+2;
        while(++x <= 10);
        //if(x > 10)
        //{
            //x = 0;
        //}
    }
}

Here it can work but without the loop.

integer x;
default
{
    touch_start(integer total_number)
    {
        //integer x;
        //do
            llSay(0, (string)x);
            x = x+2;
        //while(++x <= 10);
        if(x > 10)
        {
            x = 0;
        }
    }
}

I have been trying many syntax but there is things I don't understand.

Link to comment
Share on other sites

   

    touch_start(integer total_number)
    {
        integer x;
        do
        {
             llSay(0, (string)x);
             x += 2;
        }
        while ( x < 12);
    }

 

  • Like 1
Link to comment
Share on other sites

*Slam my head on the desk*

Well, thank you very much Rolig Loon. I did so many typo close to that but I was having syntax error again and again. I have understanding keys missing when it's about scripting learning only by reading the wiki and trying.

Link to comment
Share on other sites

Rolig's example is probably better practice and harder to mess up, but you can put the increment inside the while condition:

integer i
do	{
	llSay(0,(string)i);
}while((i=i+2)<=10);

The important things to take note of are the parentheses, and that you need to /not/ use '+='. This works because the return value of '=' is the right hand argument, which is done to allow things like:

integer i;
integer j;
integer k;

i=j=k=7;

to set several variables to the same value.

Equality has the lowest operator precedence however, so it's wise to be very explicit with parentheses. actually, will probably save you in many cases, but it's good to be explicit.

Edited by Quistess Alpha
  • Like 1
Link to comment
Share on other sites

Coding style is largely a matter of personal choice, certainly in SL. There are so many acceptable ways to write the same flow control sequence that I find it hard to argue that any of them is "best". Personally, I tend to use while rather than do while or for most of the time, but my choice often comes down to a whim of the moment. 

The same is true for deciding how and where to increment a counting variable.  My first mentor in LSL was Void Singer, who favored a very compact -- even terse -- style.  As a result, I often write in-line incremental functions, as you have with while((i=i+2)<=10). The downside is that subtle bits of logic can be easy to overlook when you are trying to debug or modify a script.  If I am writing a script that already has a bit of complex logic (or if I am writing something to be read by a novice), then, I may make things easy for myself by using a more open style.  Quite coincidentally, I just finished updating a 600+ line script that I wrote a few years ago -- cursing all the time because I had made it hard to see when a couple of key variables were supposed to increment.

  • Like 3
Link to comment
Share on other sites

3 hours ago, Gadox said:

Well, thank you very much Rolig Loon. I did so many typo close to that but I was having syntax error again and again. I have understanding keys missing when it's about scripting learning only by reading the wiki and trying.

Syntax errors are usually a missing ' ; ' from the line above, or, from seeing your code it was probably missing brackets.

When your loop has a single statement, you can just write it as:

do
 llOwnerSay("Hello");
while(--n);

But when there are additional statements it must be enclosed within the curly brackets. Save yourself the headache and always use the brackets, whether for a single or multiple statements:

do
{
	llOwnerSay("Hello");
	llOwnerSay("World");
}while(--n);

 

  • Like 2
Link to comment
Share on other sites

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...