Skip to main content
Version: 7.9

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

    • object value - A value of any type.

    • object case - A case to match the value to.

    • object return - The return if its pair case has been matched.

    • object returnDefault - The default return if none of the case arguments were matched.

  • Results

    • 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
1, // case 1
44, // return 1
24, // case 2
45, // return 2
15, // case 3
46, // return 3
-1) // default
Code Snippet
//The following would return "Running".
case(
1, // value
0, // case 1
"Off", // return 1
1, // case 2
"Running", // return 2
2, // case 3
"Fault", // return 3
forceQuality("!BAD STATE!",0)) // default