switch

Arguments:
any, value 1, statement 1, [value 2, statement 2, [...]]
Return Value:
from executed statement

The switch statement chooses and executes a piece of MUDL code based on the value of an expression. It is very similar to the if function, differing only in that instead of choosing from two alternatives it chooses from many.

Switch takes a variable number of arguments, always at least three. The first argument is an expression that is always evaluated. Switch then compares the result of this evaluation with the second argument, the fourth argument, the sixth argument, and so on, until it runs out of arguments. (This means that these arguments also get evaluated). Comparison is done with = . When switch finds a match between the first argument's value and the value of one of the even-numbered arguments, it then evaluates the next argument following that even-numbered argument.

Switch itself evaluates to the value of whatever argument it chose. If it didn't find ANY match, it evaluates to null. The type of the null is the type of the unexecuted odd-numbered arguments (which should all have the same type).

For example, say we want to randomly increase or decrease %c's move, with a 25% chance of increasing it 1d10 points and a 25% chance of decreasing it 1d10 points. We could say:
   switch(1d4,
      1,set(move(%c),move(%c)+1d10),
      2,set(move(%c),move(%c)-1d10)
   )
Every switch statement can be done with nested if statements. In this case, the same function could be achieved as follows with if statements:
   if(1d4=1,
      set(move(%c),move(%c)+1d10),
      if(1d3=1,
         set(move(%c),move(%c)-1d10)
      )
   )
Notice how the switch version is clearer. Also, note that to get the probabilities to come out the same when evaluating two die-rolls in the if version, the second roll was a 1d3.