Don't click here unless you want to be banned.

LSL Wiki : CrashCourse6

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are ia360925.us.archive.org
Chapter Listing
< 1 2 3 4 5 6 7 8 9 >

6) Statements, conditionals (meat and potatoes 1)
Now we get to the part of programming that actually accomplishes something -- the logic. Everything else is more of a framework to allow us to describe the organization of the code, more or less.

Fundamentally, a computer program takes input, make a decision based on some conditions, and produces output. Often that output is fed right back in for further decision-making.

Let's take a look at a simple, yet non-trivial, conditional block:
// assume x has already been declared, as an integer
if (x < 10)
{
   x = x + 1;
}
else
{
   x = 0;
}

Can you guess the range of values we expect to see in x? Indeed, it's likely that x would have a value between (and including) zero and 10, at any given time. Basically, it says "if we haven't reached ten with x, add one to x, and store the result back into x, effectively incrementing x by one; otherwise, reset x to zero." It's rather a bit like English, isn't it? Once the symbols become more familiar to you, it will be easy to read the code, and equally easy to write your own. Notice the statement x = x + 1;, which is basically saying "x now has a value of whatever it had plus one".

Notice also something else (pun intended, hah!) -- the else keyword. If the first condition fails (that is, if x is not less than 10), then the first code block is skipped, and control is passed to the next block. If that else is immediately followed by an if, then the following code-block will also be skipped if the condition is not met. And yes, you can set up "chains" of cascading conditionals like so:
// assume s has already been declared, as a string
if (s == ":-)")
{
   llOwnerSay("You seem to be in a good mood.");
}
else if (s == ":-(")
{
   llOwnerSay("Hey champ, what's got you down?");
}
else
{
   llOwnerSay("Your expression is mystifying.  What's on your mind, homie?");
}

Makes sense?

Right, so, basically, many scripts will figure out where things are, what color they are, how big they are, etc, and change some of those things. For instance, if you build a security door, it might "pay attention" for a command word from an avatar, and then it will react by modifying its position or rotation to allow passage. Naturally, it's going to need some conditionals -- it might check if the command came from the owner, rather than just some random passerby. Or it might check if it is already open. I'm sure you get the idea.

Two more things -- first, a function call that doesn't care about the return value (AKA a procedure call) can usually be placed anywhere a statement can, and sometimes can be loosely referred to as a statement; second, if you only have one statement after your conditional (and each of its else(s), if any), you can omit the curly braces. Here is the first example again, without the braces:
// assume x has already been declared, as an integer
if (x < 10)
   x = x + 1;
else
   x = 0;

Previous | Next

Homepage | Tutorial | CrashCourse | Glossary
There is one comment on this page. [Display comments/form]