Difference between revisions of "If/fr"

From Second Life Wiki
< If
Jump to navigation Jump to search
(Prepared for translation by Fana Dechou)
 
m (At least let's not pollute English categories)
Line 4: Line 4:
}}{{#vardefine:p_condition_d_desc| If this executes as true then '''branch_true''' is executed otherwise '''branch_false''' is executed.
}}{{#vardefine:p_condition_d_desc| If this executes as true then '''branch_true''' is executed otherwise '''branch_false''' is executed.
}}{{#vardefine:p_branch_desc|Can be either a single statement, a block statement, or a null statement.
}}{{#vardefine:p_branch_desc|Can be either a single statement, a block statement, or a null statement.
}}{{LSL_Conditional
}}{{LSL_Conditional/fr
|statement=if
|statement=if
|p1_name=condition
|p1_name=condition
Line 131: Line 131:
|mode
|mode
|deprecated
|deprecated
|cat1=Conditional
|cat1=Conditional/fr
|cat2
|cat2
|cat3
|cat3
|cat4
|cat4
}}
}}

Revision as of 07:28, 7 December 2007

if ( condition ) branch

•  condition If this executes as true then branch is executed.
•  branch Can be either a single statement, a block statement, or a null statement.

if ( condition ) branch_true else branch_false

•  condition If this executes as true then branch_true is executed otherwise branch_false is executed.
•  branch_true Can be either a single statement, a block statement, or a null statement.
•  branch_false Can be either a single statement, a block statement, or a null statement.

Spécification

Conditions par type
Type Condition
integer Vrai si non nul.
float Vrai si non nul.
string Vrai si sa longueur est non nulle.
key Vrai seulement s'il s'agit d'une clé valide et différente de NULL_KEY.
vector Vrai si le vecteur est différent de ZERO_VECTOR.
rotation Vrai si la rotation est différente de ZERO_ROTATION.
list Vrai si sa longueur est non nulle.

Operators in LSL do NOT short circuit. For example:

// A simple method to say if the method was called.
integer test() {
    llOwnerSay("Test method called!");
    
    return TRUE;
}

default {
    touch_start(integer total_number) {
        if (FALSE && test()) { // If short-circuit optimized then the test() should never be called.
            // Will never get here.
        }
    }
}

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.
            }
        }
    }
}

Exemples

Simple 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:

  1. The line if (a == "Loren"); will execute; the semicolon will tell LSL that this is a simple inline if statement.
  2. Next it meets the start of the code block and will execute the contents, in this case saying "Lorem ipsum sic amet!", no matter what the outcome of the above if statement.

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 a was equal to "Joe" then all that would be said is "I don't know what it means either..."