Skip to main content
Version: 8.1

switch

This function is used by Ignition's Expression language.

Description​

This function acts like the switch statement in C-like programming languages. It takes the value argument and compares it to each of the case1 through caseN expressions. If value is equal to caseX, then switch returns valueX. If value is not equal to any of the case1..N, then returnDefault is returned.

Note that switch() is similar in functionality to the case() expression function. The difference between the two is the order in which the parameters are passed.

Syntax​

switch(value, case, [caseN, ...], return, [returnN, ...], returnDefault)

  • Parameters

    • Object value - The value to check against the case values.

    • Object case - A value to check against. Can be any number of case values.

    • Object return - A value to return for the matching case. Must be the same number of return values as case values.

    • Object returnDefault - The default return if no case is matched.

  • Results

    • Object - The return value for the case that matched the value, or the returnDefault value if no matches were found.

Examples​

Code Snippet
//The following would return 46 because the value (15) matched case 3, so the third return (46) was returned.
switch(
15, // value
1, // case 1
24, // case 2
15, // case 3
44, // return 1
45, // return 2
46, // return 3
-1) // default
Code Snippet
//The following would return "Running".
switch(
1, // value
0, 1, 2, // cases 1-3
"Off", // return 1
"Running", // return 2
"Fault", // return 3
"BAD STATE!") // default