Help:Switch parser function

The switch parser function, coded as "#switch", selects the first matching branch in a list of choices, acting as a case statement.

Each branch can be a value, an expression (calculation), or a template call, evaluated and compared to match the value of the switch. Although many #switch structures are used to branch among a simple set of values, the branches can also include boolean expressions to act as a set of pre-conditions to be tested until one tests true, acting as an if-elseif-elseif-elseif-else structure. The switch value is compared as a character string, not a number, so "5" would not match "5.00" unless compared as.

A #switch can contain over 1,000 branches, but for better speed should be split to have less than 100 branches, in each of multiple or nested parts. The upper branches execute faster, and the bottom branches must wait for comparisons of all higher branches. Because it is a parser function, a #switch can be used inside any page, even in article text, but in most cases, a #switch is typically used inside a template, to branch depending on a parameter value passed to the template.

General format
The #switch function can be a one-line form:

That one-line #switch would read the value of. For a value of 1 it would return "one". For a value of 2 it would return "two". For the values 3, 4 or 5 it would return "range 3–5". For any other value, or a null value, it would return "other".

However, in many cases, the #switch function is a multi-line form, with each branch on a different line, as follows:

This illustrates an alternative method of specifying the default case, which can appear first, last, or anywhere in between.

If no default is specified and no case matches the supplied value, a null value is returned.

For each branch of a #switch, either side of an equals-sign "=" can be a simple value, an expression, or a template call.

Using switches as if-elseif-elseif
A #switch function can be structured as a set of pre-conditions which are tested until one is true (equal to "1"). For example to pre-screen numbers to avoid division by zero, in formula: x/x2 + y/y2 When testing x2 and y2, if either of them is zero (none), then the #switch ends with a warning message, rather than calculating the weighted average of the two amounts x, y, with x2 and y2. Each branch acts as a pre-condition, so the whole #switch structure performs as equivalent to if-elseif-elseif-elseif-else, even though an #if-function structure cannot have an "elseif" clause. Note the 2 types of data compared:


 * Alphabetic data
 * An alpha character string can be compared in the  by using   as follows:    ,
 * where the result would be "1" when true, or "0" when false.


 * Numeric data
 * Some numbers can be compared in the  by using   as follows:     or
 * where each result would be "1" when true, or "0" when false.

Another example, to test an amount "n" to determine the number of decimal digits: That is another example, using "#switch: 1" to stack a set of pre-conditions which are tested, in sequence, until one is true (equal to "1").

Performance considerations
Templates using such convoluted switch statements as to suffer significant performance issues should be translated to Lua, if that will make them simpler or significantly faster. The below discussion should rarely be relevant but is preserved for reference.

A #switch can contain over 1,000–2,000 branches, but should be split to have less than 100 branches, in multiple or nested parts. In some cases, it might be possible to split into multiple #switch structures, such as when many cases use the same first letter. Then, using  can extract the first letter of   to be used in a higher #switch which branches by most-frequent letter, followed by all others at the bottom or "#default" branch. Another common split might be to separate numeric values into frequent ranges, followed by all other.

The upper branches of a #switch execute faster, and the bottom branches must wait for comparisons of all higher branches. Hence, the top branch should be the most-used case, unless pre-screening of data is done in the early branches.