Difference between revisions of "Switch Statement"

From Second Life Wiki
Jump to: navigation, search
 
Line 4: Line 4:
  
 
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.
 
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.
 +
 +
For now, there is a way to simulate a switch statement by using '''if''' structure with '''return'''. See this example:
 +
 +
<lsl>
 +
    listen(integer channel, string name, key id, string message)
 +
    {
 +
// If the first condition is true, the second condition won't be evaluated, like a "switch" would do.
 +
        if (channel == PUBLIC_CHANNEL) {
 +
            //Any statements here
 +
            return;
 +
        }
 +
       
 +
// 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;
 +
    }
 +
 +
// Using "else" in this structure may not reach the same result.
 +
// Also, it was verified this method prevents lag, differently from use of "else"
 +
</lsl>
 +
 +
Although a switch statement would be helpful.
  
 
[[Category:LSL WishList | LSL WishList]]
 
[[Category:LSL WishList | LSL WishList]]

Latest revision as of 08:20, 10 March 2012

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_*)

mod:Sedric Raymaker-- it would also cut down on huge nested if statements, little surprised is not available, but can work around for now

A JIRA feature request exists at VWR-1287. Please go vote for it if this is important to you.

For now, there is a way to simulate a switch statement by using if structure with return. See this example:

<lsl>

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

// If the first condition is true, the second condition won't be evaluated, like a "switch" would do.

       if (channel == PUBLIC_CHANNEL) {
           //Any statements here
           return;
       }
       

// 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;

   }

// Using "else" in this structure may not reach the same result. // Also, it was verified this method prevents lag, differently from use of "else" </lsl>

Although a switch statement would be helpful.