If
| LSL Portail Francophone | LSL Portail Anglophone | Fonctions | Évènements | Types | Operateurs | Constantes | Contrôle d'exécution | Bibliothèque | Tutoriels |
if ( condition ) branch
| |||||||||||||||||
if ( condition ) branch_true else branch_false
| |||||||||||||||||
Spécification
Operators in LSL do NOT short circuit. For example:
In a short-circuited language if the left-hand side of a logical AND (&&) is FALSE the right-hand side is never tested since the entire test would always return a FALSE output no matter what the value of the right-hand side. Since LSL is not short circuited, both the left and the right-hand sides are tested all the time. This can be simulated by using a nested if structure: default {
touch_start(integer total_number) {
if (FALSE) {
if (test()) {
// Will never get here.
}
}
}
}
| |||||||||||||||||
ExemplesSimple inline if statement: if (a == 1) c = b; Simple block if statement: if (a == 1)
{
// Do something here.
}
Complex if/else block (only one line of text will be said by this example) if (a == "Loren") {
llSay(0, "Lorem ipsum sic amet!");
} else if (a == "Bob") {
llSay(0, "Babble dabble rabble rouse.");
} else {
llSay(0, "Gobbledygook? or English?");
}
Compounded if statement: if (a == 1 && b == c)
{
// more here.
}
Nested if statements: if (a == 1)
{
if (b == c)
{
// more here.
}
}
Watch out for misplaced semicolons. They can, and will, cause strange things to happen. For instance: If you place a semicolon between an if statement and a code block, the if statement will not control the execution of the block. if (a == "Loren");
{
llSay(0, "Lorem ipsum sic amet!");
}
This code will execute in the following order:
Another problem happens when someone forgets to use the curly braces. if (a == "Loren")
llSay(0, "Lorem ipsum sic amet!");
llSay(0, "I don't know what it means either...");
The assumption is that because the indentation makes it "look" like both llSay commands are controlled by the if statement, that they are. In fact, if |
If