switch(
KEY
,
CASE
,
VALUE
,
,
DEFAULT
)

The switch( function chooses from a list of values.


Parameters

This function has four required parameters:

key – The value that controls which option is selected.

case – The value that must match the key value to select this option. This may be numeric or text, but should match the key value.

value – The value associated with this case.

– One or more additional case/value pairs may be inserted here

default – The default value that will be used if the key does not match any of the cases.


Description

The switch( function makes it easy to choose from a list of values. The first parameter is the key. The function attempts to match the key value with each of the case values (from left to right). If it finds a match, the function returns the corresponding value. If there are no matches at all, the default value is returned. (Each case/value pair is shown on a separate line in the examples below, but this is not necessary.)

switch(1,
    1,"Gold",
    2,"Silver",
    3,"Bronze",
    "-") ☞ Gold
switch(2,
    1,"Gold",
    2,"Silver",
    3,"Bronze",
    "-") ☞ Silver
switch(7,
    1,"Gold",
    2,"Silver",
    3,"Bronze",
    "-") ☞ -
switch("Gold",
    "Gold",79,
    "Silver",47,
    "Copper",29,
    "Tin",50,
     0) ☞ 79
switch("Copper",
    "Gold",79,
    "Silver",47,
    "Copper",29,
    "Tin",50,
     0) ☞ 29
switch("Cu",
    "Au","Gold",
    "Ag","Silver",
    "Cu","Copper",
    "Sn","Tin",
    "Unknownium") ☞ Copper
switch("Nx",
    "Au","Gold",
    "Ag","Silver",
    "Cu","Copper",
    "Sn","Tin",
    "Unknownium") ☞ Unknownium

As shown in the examples above, the key and case values can be either text or numbers (you can even mix both text and numbers, though this rarely makes sense). The returned value parameters can also be either text or numbers (again, it usually makes sense for all of them to be the same type).


See Also


History

VersionStatusNotes
10.0NewNew in this version.