case
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 case returns valueX. If value is not equal to any of the case1..N, then returnDefault is returned.
Note that case() is similar in functionality to the switch() expression function. The difference between the two is the order in which the parameters are passed.
Syntax​
case(value, case, return[, case, return...], returnDefault)
Parameters​
Type | Parameter | Description |
---|---|---|
Object | value | The value to compare against each case. |
Object | case | A case to match the value to. |
Object | return | The value to return if the corresponding case is matched. |
Object | returnDefault | The default return value if no case arguments are matched. |
Returns​
Object - The return value for the matched case, or the returnDefault value if no case was matched.
Examples​
Code Snippet
//The following would return 46 because the value (15) matched case 3, so the third return (46) was returned.
case(
15, // value to inspect
1, 44, // case 1, return 1
24, 45, // case 2, return 2
15, 46, // case 3, return 3
-1 // default return
)
Code Snippet
//The following would return "Running".
case(
1, // value to inspect
0, "Off", // case 1, return 1
1, "Running", // case 2, return 2
2, "Fault", // case 3, return 3
"BAD STATE!" // default return
)