If, else in expression or use jython?

Is it possible to do an If, Else If, Else in expression, or should I move to using the jython scripting for that.

Case, I have 2 inputs,4 seperate cases, one true, other one true, both false.(both true is something i’d like to catch as an error)

You can do a nested if like this in the expression language:

if ({input1} && !{input2}, "input 1 is true", if (!{input1} && {input2}, "input 2 is true", if(!{input1} && !{input2}, "neither is true", "both are true" )))

But a nicer way to do what you’re doing is to feed the booleans through the “binEnc” expression which will turn the two booleans into an integer (0-3):

switch(binEnc({input1}, {input2}), 0,1,2,3, "neither are true", // 00 "input 1 is true", // 01 "input 2 is true", // 10 "both are true", // 11 "impossible")

Hope this helps,

3 Likes

Ahh, I’ve been dealing with ladder too long. Converting to base 2 never occcured to me. Thanks!

It’s really amazing how in 2020 we are still having issues with elif and expressions, has any made good use of the switch statement?

The closest expression equivalent of elif is nested if statements. It's not quite as elegant and must include a final else (unlike Python scripts):

// Start with an if.
if(condition, 1,
	// Nest if to get equivalent of elif.
	if(condition, 2,
		// With expressions you always have an else value at the end.
		0
	)
)

In most cases, binEnum would be an elegant replacement for the above nested ifs. See examples in the other thread you posted in.

The switch expression isn't a viable replacement for elif unless you're using elif to evaluate different values of a single condition.

That works very well, how do I add a three more tags and increment the output 3, 4, 5 ?

binEnum(condition, condition2, condition3, condition4)
>>>
binEnum(true, false, false, false) -> 1
binEnum(false, true, false, false) -> 2
binEnum(false, false, false, false) -> 0
1 Like