# If Statement

**URL:** <https://forum.inductiveautomation.com/t/if-statement/6102>\
**Category:** Ignition\
**Created:** [March 26, 2013, 1:57am UTC](https://forum.inductiveautomation.com/t/if-statement/6102 "2013-03-26T01:57:14Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![rewing](https://avatars.discourse-cdn.com/v4/letter/r/b782af/32.png) [@rewing](https://forum.inductiveautomation.com/u/rewing)\
**Post date:** [March 26, 2013, 1:57am UTC](https://forum.inductiveautomation.com/t/if-statement/6102/1 "2013-03-26T01:57:14Z")

</div>

I have the following in a Transaction Group Triggered Expression Item.

I would like the Value of the expression Items to change to the names based on the left most charater of the serial number.

What is wrong with the following If statement??

if (left{[~]SerialNumber}) = ‘A’:  
‘Lambda’  
elif (left{[~]SerialNumber}) = ‘B’:  
‘LambdaCover’  
elif(left{[~]SerialNumber}) = ‘C’:  
‘0351’  
Else:  
‘Unknown’

Thanks In Advance!

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 26, 2013, 9:44am UTC](https://forum.inductiveautomation.com/t/if-statement/6102/2 "2013-03-26T09:44:41Z")

</div>

Hi!

Expression Items aren’t Python based. For multiple If…Then conditions, consider using the Switch function:

`SWITCH ( left({[~]SerialNumber},1), 
        "A", "B", "C",
        "Lambda","LambdaCover","0351",
        "Unknown"
        )
`

Information on the Switch function can be found [here](http://inductiveautomation.com/support/usermanuals/ignition/gen_logic_switch.htm).

---

<div class="post-metadata">

**Author:** ![rewing](https://avatars.discourse-cdn.com/v4/letter/r/b782af/32.png) [@rewing](https://forum.inductiveautomation.com/u/rewing)\
**Post date:** [March 27, 2013, 12:14am UTC](https://forum.inductiveautomation.com/t/if-statement/6102/3 "2013-03-27T00:14:51Z")

</div>

Thanks,

I Get the diff between Exp and Scripting now I think…

Sorry for being such a newb!!..

---

<div class="post-metadata">

**Author:** ![Nelly](https://avatars.discourse-cdn.com/v4/letter/n/ed8c4c/32.png) [@Nelly](https://forum.inductiveautomation.com/u/Nelly)\
**Post date:** [September 12, 2017, 8:03am UTC](https://forum.inductiveautomation.com/t/if-statement/6102/4 "2017-09-12T08:03:24Z")

</div>

Hi, Ive got a similar issue, I can get a simple IF to work, to bind the colour of a text to a condition,

IF(({Brac Tags/Analog\_In/PET\_150}\>50||{Brac Tags/Analog\_In/PET\_150}\<25),color(217,0,0),color(0,0,0))

But I want a few more conditions than this,and trying to make the simple one a SWITCH from above or a few tries similar doesn’t work, gives me Switch does not work with multiple arguments or similar…Also, what is the ,1 for end of line 1…I tried the link to the manual it doesn’t work, and speaking of, where is the manual mentioned often in training…

SWITCH ({Brac Tags/Analog\_In/PET\_150},1,  
\>50,\<25,  
color(217,0,0),color(217,0,0),  
color(0,0,0)  
)

It doesn’t like the greater than line 2, and If I put full tag string in there it says switch doesnt work with 7 arguments.  
Thanks  
Neil

---

<div class="post-metadata">

**Author:** ![accyroy](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/accyroy/32/3386_2.png) [@accyroy](https://forum.inductiveautomation.com/u/accyroy)\
**Post date:** [September 12, 2017, 10:21am UTC](https://forum.inductiveautomation.com/t/if-statement/6102/5 "2017-09-12T10:21:59Z")

</div>

You can’t evaluate a greater than or less than in a switch function. A workaround as per [this thread](http://forum.inductiveautomation.com/t/expression-language-switch-upgrade/549/5) is to use binEnum like this:

```auto
switch(
  binEnum({Brac Tags/Analog_In/PET_150}=1,
          {Brac Tags/Analog_In/PET_150}>50,
          {Brac Tags/Analog_In/PET_150}<25),
1,2,3,
'color(217,0,0)', //result 1 
'color(217,217,0)', //result 2 
'color(0,0,0)', //result 3 
'none') //default

```

The manual page for Switch is [here](https://docs.inductiveautomation.com/display/DOC79/switch).

---

<div class="post-metadata">

**Author:** ![Nelly](https://avatars.discourse-cdn.com/v4/letter/n/ed8c4c/32.png) [@Nelly](https://forum.inductiveautomation.com/u/Nelly)\
**Post date:** [September 12, 2017, 10:47am UTC](https://forum.inductiveautomation.com/t/if-statement/6102/6 "2017-09-12T10:47:23Z")

</div>

Well Thank you Sir That is much appreciated, I’ll give it a go and check that thread also,  
Cheers, Neil

---

<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 30, 2020, 7:11pm UTC](https://forum.inductiveautomation.com/t/if-statement/6102/7 "2020-01-30T19:11:42Z")

</div>

This is a broken link, please provide some information on a switch case .

---

<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 30, 2020, 7:37pm UTC](https://forum.inductiveautomation.com/t/if-statement/6102/8 "2020-01-30T19:37:31Z")

</div>

You’ll find a link to switch under the logic section of [Expression Functions documentation](https://docs.inductiveautomation.com/display/DOC80/Expression+Functions) in the online Ignition user manual.

---

<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:17pm UTC](https://forum.inductiveautomation.com/t/if-statement/6102/9 "2020-01-31T18:17:36Z")

</div>

So we need an if statement for 3 tags, should we look at the switch statement or binEnum? Neither appear to work.

---

<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, 6:18pm UTC](https://forum.inductiveautomation.com/t/if-statement/6102/10 "2020-01-31T18:18:57Z")

</div>

Show us what you’re trying, what you’re getting, and what you’d like to get.

---

<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:21pm UTC](https://forum.inductiveautomation.com/t/if-statement/6102/11 "2020-01-31T18:21:09Z")

</div>

IF(({[.]…/CTR\_VAC\_ROLL\_A\_MODE}\>0||{[.]…/CTR\_VAC\_ROLL\_B\_MODE\_TENSION}\>0),1,2)

Always returns “1”

---

<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:23pm UTC](https://forum.inductiveautomation.com/t/if-statement/6102/12 "2020-01-31T18:23:06Z")

</div>

Trying to setup a multistate indicator with more than 1 - input. I need a case switch statement I can plug into a memory location(via expression)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 31, 2020, 6:27pm UTC](https://forum.inductiveautomation.com/t/if-statement/6102/13 "2020-01-31T18:27:22Z")

</div>

I see an “or” operator in there. Ignition’s expression order of operation isn’t normal. Try adding parentheses around your comparisons.

---

<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, 6:49pm UTC](https://forum.inductiveautomation.com/t/if-statement/6102/14 "2020-01-31T18:49:17Z")

</div>

> [@AFULLER](#):
>
> IF(({[.]…/CTR\_VAC\_ROLL\_A\_MODE}\>0||{[.]…/CTR\_VAC\_ROLL\_B\_MODE\_TENSION}\>0),1,2)

Do you want a result of 1 if either of your first conditions is true and 2 if neither is true? If so, what you have should work, assuming the relative tag paths are correct.

Or do you want a result of 1 if the first condition is true and a result of 2 if the second condition is true? There are multiple ways to do this, depending on what result you want if both conditions are true or both are false. Here's an example using `if` that ignores the second condition if the first condition is true:

```java
// Return 1 if first condition is true (regardless of second condition).
if({[.]…/CTR_VAC_ROLL_A_MODE}>0, 1,
	// Return 2 if first condition is false and second condition is true.
	if({[.]…/CTR_VAC_ROLL_B_MODE_TENSION}>0, 2,
		// Return zero if neither is true.
		0
	)
)

```

A simpler way to get the same result would use [binEnum](https://docs.inductiveautomation.com/display/DOC80/binEnum):

```java
// Returns index of first true condition:
// 0 indicates both conditions false.
// 1 indicates first condition true, regardless of second condition.
// 2 indicates second condition true and first condition is false.
binEnum(({[.]…/CTR_VAC_ROLL_A_MODE}>0,{[.]…/CTR_VAC_ROLL_B_MODE_TENSION}>0)

```

Or use [binEnc](https://docs.inductiveautomation.com/display/DOC80/binEnc) to capture all possible states:

```auto
// 0 (binary 00) indicates both conditions false.
// 1 (binary 01) indicates first condition true.
// 2 (binary 10) indicates second condition true.
// 3 (binary 11) indicates both conditions true.
binEnc(({[.]…/CTR_VAC_ROLL_A_MODE}>0,{[.]…/CTR_VAC_ROLL_B_MODE_TENSION}>0)

```

---

<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, 6:55pm UTC](https://forum.inductiveautomation.com/t/if-statement/6102/15 "2020-01-31T18:55:39Z")

</div>

> [@AFULLER](#):
>
> should we look at the switch statement or binEnum

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:** [May 27, 2020, 9:04pm UTC](https://forum.inductiveautomation.com/t/if-statement/6102/16 "2020-05-27T21:04:07Z")

</div>

This worked well for me, thanks for the help.

switch(  
binEnum({[.]M22\_VacuumRollEDraw/TAG\_VAL}=1,  
{[.]M22\_VacuumRollETension/TAG\_VAL}=1,  
{[.]M22\_VacuumRollETorque/TAG\_VAL}=1),  
1,2,3,  
1, //result 1  
2, //result 2  
3, //result 3  
99) //default
