# If, else in expression or use jython?

**URL:** <https://forum.inductiveautomation.com/t/if-else-in-expression-or-use-jython/860>\
**Category:** Archived Topics\
**Created:** [October 30, 2008, 8:41pm UTC](https://forum.inductiveautomation.com/t/if-else-in-expression-or-use-jython/860 "2008-10-30T20:41:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dravik](https://avatars.discourse-cdn.com/v4/letter/d/e274bd/32.png) [@Dravik](https://forum.inductiveautomation.com/u/Dravik)\
**Post date:** [October 30, 2008, 8:41pm UTC](https://forum.inductiveautomation.com/t/if-else-in-expression-or-use-jython/860/1 "2008-10-30T20:41:57Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [October 30, 2008, 9:48pm UTC](https://forum.inductiveautomation.com/t/if-else-in-expression-or-use-jython/860/2 "2008-10-30T21:48:54Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![Dravik](https://avatars.discourse-cdn.com/v4/letter/d/e274bd/32.png) [@Dravik](https://forum.inductiveautomation.com/u/Dravik)\
**Post date:** [October 31, 2008, 12:23pm UTC](https://forum.inductiveautomation.com/t/if-else-in-expression-or-use-jython/860/3 "2008-10-31T12:23:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AFULLER](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/afuller/32/7931_2.png) [@AFULLER](https://forum.inductiveautomation.com/u/AFULLER)\
**Post date:** [January 31, 2020, 6:13pm UTC](https://forum.inductiveautomation.com/t/if-else-in-expression-or-use-jython/860/4 "2020-01-31T18:13:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [January 31, 2020, 7:15pm UTC](https://forum.inductiveautomation.com/t/if-else-in-expression-or-use-jython/860/5 "2020-01-31T19:15:17Z")

</div>

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):

```java
// 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](http://forum.inductiveautomation.com/t/if-statement/6102/14) 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.

> [@If Statement](http://forum.inductiveautomation.com/t/if-statement/6102/15):
>
> Use [switch](https://docs.inductiveautomation.com/display/DOC80/switch) or [case](https://docs.inductiveautomation.com/display/DOC80/case) to get different results for different values of a single tag/property. Use [binEnum](https://docs.inductiveautomation.com/display/DOC80/binEnum) (to get index of first true condition) or [binEnc](https://docs.inductiveautomation.com/display/DOC80/binEnc) (to get a single value representing state of all conditions) for multiple true/false conditions.

---

<div class="post-metadata">

**Author:** ![AFULLER](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/afuller/32/7931_2.png) [@AFULLER](https://forum.inductiveautomation.com/u/AFULLER)\
**Post date:** [January 31, 2020, 9:48pm UTC](https://forum.inductiveautomation.com/t/if-else-in-expression-or-use-jython/860/6 "2020-01-31T21:48:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [January 31, 2020, 10:12pm UTC](https://forum.inductiveautomation.com/t/if-else-in-expression-or-use-jython/860/7 "2020-01-31T22:12:10Z")

</div>

> [@witman](#):
>
> In most cases, binEnum would be an elegant replacement for the above nested ifs. See [examples](http://forum.inductiveautomation.com/t/if-statement/6102/14) in the other thread you posted in.

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

```
