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 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
)
  • No labels