Click or drag to resize
Switch

[This is preliminary documentation and is subject to change.]

The TOPICA Basic function Switch is used for "multi-way" conditional evaluation.

Syntax
Switch(Expression1, Expression2, Expression3, ...)

Switch works in 2 modes, depending on number of parameters:

Even number of parameters

In this mode, first and second parameters are treated as a pair, third and second parameter is treated as a pair, and so on.

First parameter in each pair is evaluated - type of result should be Boolean. If the evaluated valued is True, the Switch function evaluates the second parameter in the pair, and returns this value. The remaining pairs are not evaluated.

To implement a default case, add a pair at the end, where the first value is the constant true.

Example:

switch
(
    2+2=3, "2 plus 2 equals 3",
    2+2=4, "2 plus 2 equals 4",
    2+2=5, "2 plus 2 equals 5",
    true, "2 plus 2 is neither 3, 4, nor 5"
)

This expression will return the value "2 plus 2 equals 4".

Odd number of parameters

In this mode first parameter is evaluated (type of result should be Integer). This value is referred to as SwitchValue. After that, second and third parametere is treated as a pair, fourth and fifth parameter is treated as a pair, and so on.

First parameter in each pair is evaluated - type of result should be same as type of SwitchValue (i.e. Integer). If the evaluated valued is equal to SwitchValue, the Switch function evaluates the second parameter in the pair, and returns this value. The remaining pairs are not evaluated.

To implement a default case, add a pair at the end, where the first value is the constant null.

Example:

switch
(
    2+2,
    3, "2 plus 2 equals 3",
    4, "2 plus 2 equals 4",
    5, "2 plus 2 equals 5"
    null, "2 plus 2 is neither 3, 4, nor 5"
)

This expression will return the value "2 plus 2 equals 4".

See Also