Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Branching Statements

Wyatt Weatherwax
Registered User
Join date: 23 Oct 2007
Posts: 59
02-03-2009 07:54
Can someone provide me with an explanation for the various type of branching statements I have seen, like:

if XXXXX

if XXXXX

or

if xxxxx

else if xxxxx

or

if xxxxx

else xxxxx

and why one would preferentially use one type over another in a situation.

Thanks
Lee Ponzu
What Would Steve Do?
Join date: 28 Jun 2006
Posts: 1,770
02-03-2009 08:05
This is pretty fundamental programming logic, and will be covered in almost any intorductory text and is almost the same in almost every programming language.

In LSL specifically,

if ( x > 1 )
{
<do something>
}

Would compute what ever <do something> is, only if the value of x is greater than one.

he other variations are left as an exercise for the student 8-)
_____________________
So many monkeys, so little Shakespeare.
Kahiro Watanabe
Registered User
Join date: 28 Sep 2007
Posts: 572
02-03-2009 09:05
I think you want to know the different of having several separate if and using the if else structure, this can be explained with a step-by-step script run, but I will try to do that here.
integer x = 1;

Case 1:

1: if (x == 1)
{
2: llSay(0,"Hi!";);
}

3: if (x == 2)
{
llSay(0,"Bye!";);
}
4:

_________________________________________________
In Case 1 above the script will check if x equals 2. It does not so it will not enter inside the 'if'.

Case 2:

integer x = 1;

1: if (x == 1)
{
2: llSay(0,"Hi!";);
}
else if (x == 2)
{
llSay(0,"Bye!";);
}

3:
________________________________________________

In Case 2 the script ignores the second if, thanks to the 'else'. Once the value is found the rest chain of 'else if' will not be checked.

Case 3

integer x = 120938;

1: if (x == 1)
{
llSay(0,"Hi!";);
}
2: else
{
3: llSay(0,"Bye!";);
}
4:
_______________________________________________

In case 3 because the 'else' does not have any condition it will be true to any number different than 1.
_____________________
Jocko Domo japanese goods



[Blog]
Yingzi Xue
Registered User
Join date: 11 Jun 2008
Posts: 144
02-03-2009 10:18
It should be noted that the "Case x" and "x:" in the above post are not actually part of the code. You can find an explanation of the IF statement and branching here: http://wiki.secondlife.com/wiki/If
Wyatt Weatherwax
Registered User
Join date: 23 Oct 2007
Posts: 59
02-03-2009 11:49
Thanks, the examples and explanations are very helpful to this no proprammer.
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
02-03-2009 13:38
See also http://www.lslwiki.net/lslwiki/wakka.php?wakka=FlowControl
Lee Ponzu
What Would Steve Do?
Join date: 28 Jun 2006
Posts: 1,770
02-04-2009 07:15
From: Wyatt Weatherwax
Thanks, the examples and explanations are very helpful to this no proprammer.

I think you might find it useful to read through any beginner programming text, just to get the feel for things. The LSL wiki usually assumes you have a least *some* previus experience.
_____________________
So many monkeys, so little Shakespeare.
Very Keynes
LSL is a Virus
Join date: 6 May 2006
Posts: 484
02-05-2009 00:12
From: Lee Ponzu
I think you might find it useful to read through any beginner programming text, just to get the feel for things. The LSL wiki usually assumes you have a least *some* previus experience.


Here is a great place to find some reading material, all books are free and cover all levels of skill, it is a great reference source/library for programmers:

http://www.freetechbooks.com/