Difference between revisions of "Switch Statement"

From Second Life Wiki
Jump to navigation Jump to search
m (Cleaned up and polished for readability)
Line 1: Line 1:
Basic C switch statement. This would be especially useful for cycling through constants that don't overlap with each other.  (i.e. TYPE_* or ATTACH_*)
The switch case statement is a common programming construct used in many programming languages to check the value of a variable and execute different code blocks based on the value. However, in LSL, '''there is no built-in switch case statement'''.


mod:Sedric Raymaker-- it would also cut down on huge nested if statements, little surprised is not available, but can work around for now
Instead, scripters can achieve similar functionality using if-else statements or a series of if statements. This example will explain how to use if/if-else/else statements as an alternative to the switch case statement in LSL.


A JIRA feature request exists at [http://jira.secondlife.com/browse/VWR-1287 VWR-1287]. Please go vote for it if this is important to you.
==Examples==
<source lang="lsl2">
default{
  listen(integer channel, string name, key id, string message){


For now, there is a way to simulate a switch statement by using '''if''' structure with '''return'''. See this example:
    if(message=="1"){
 
      llOwnerSay("The case is 1.");
<lsl>
    }
     listen(integer channel, string name, key id, string message)
     else if(message=="2"){
    {
      llOwnerSay("The case is 2.");
// If the first condition is true, the second condition won't be evaluated, like a "switch" would do.
    }
        if (channel == PUBLIC_CHANNEL) {
    else if(message=="3"){
            //Any statements here
      llOwnerSay("The case is 3.");
            return;
    }
        }
    else{
       
      llOwnerSay("The case is"+message+".");
// The second "if" will be executed only if the first condition is false, like a "switch" would do.
        if (channel == DEBUG_CHANNEL) {
            //Any statements here
            return;
        }
return;
     }
     }
    //Only one of the statements will be accepted and ignores the rest.
    //Script can continue further in this event scope without the use of 'return'.
  }
}
</source>


// Using "else" in this structure may not reach the same result.
Alternatively, using 'return' call to "break" out of the current scope. If the first condition is true, the second condition won't be evaluated, like a "switch" would do. Second "if" will be executed only if the first condition is false. Using "else" in this structure may not reach the same result.
// Also, it was verified this method prevents lag, differently from use of "else"
<source lang="lsl2">
</lsl>
default{
  listen(integer channel, string name, key id, string message){
   
    if(channel == PUBLIC_CHANNEL){
      llOwnerSay("The message was sent on Public Channel.");
      return;
    }     


Although a switch statement would be helpful.
    if(channel == DEBUG_CHANNEL) {
      llOwnerSay("The message was sent on Debug Channel.");
      return;
    }
  }
}
</source>


<s>A JIRA feature request exists at [http://jira.secondlife.com/browse/VWR-1287 VWR-1287].</s> (Has been closed.)
[[Category:LSL WishList | LSL WishList]]
[[Category:LSL WishList | LSL WishList]]

Revision as of 22:53, 30 December 2023

The switch case statement is a common programming construct used in many programming languages to check the value of a variable and execute different code blocks based on the value. However, in LSL, there is no built-in switch case statement.

Instead, scripters can achieve similar functionality using if-else statements or a series of if statements. This example will explain how to use if/if-else/else statements as an alternative to the switch case statement in LSL.

Examples

default{
  listen(integer channel, string name, key id, string message){

    if(message=="1"){
      llOwnerSay("The case is 1.");
    }
    else if(message=="2"){
      llOwnerSay("The case is 2.");
    }
    else if(message=="3"){
      llOwnerSay("The case is 3.");
    }
    else{
      llOwnerSay("The case is"+message+".");
    }
    //Only one of the statements will be accepted and ignores the rest.
    //Script can continue further in this event scope without the use of 'return'.
  }
}

Alternatively, using 'return' call to "break" out of the current scope. If the first condition is true, the second condition won't be evaluated, like a "switch" would do. Second "if" will be executed only if the first condition is false. Using "else" in this structure may not reach the same result.

default{
  listen(integer channel, string name, key id, string message){
    
    if(channel == PUBLIC_CHANNEL){
      llOwnerSay("The message was sent on Public Channel.");
      return;
    }      

    if(channel == DEBUG_CHANNEL) {
      llOwnerSay("The message was sent on Debug Channel.");
      return;
    }
  }
}

A JIRA feature request exists at VWR-1287. (Has been closed.)