# Using Switch Statement to move different values into a numeric label

**URL:** <https://forum.inductiveautomation.com/t/using-switch-statement-to-move-different-values-into-a-numeric-label/48852>\
**Category:** Ignition\
**Created:** [July 14, 2021, 7:59pm UTC](https://forum.inductiveautomation.com/t/using-switch-statement-to-move-different-values-into-a-numeric-label/48852 "2021-07-14T19:59:19Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [July 14, 2021, 7:59pm UTC](https://forum.inductiveautomation.com/t/using-switch-statement-to-move-different-values-into-a-numeric-label/48852/1 "2021-07-14T19:59:19Z")

</div>

Hello everyone,

I am trying to use the switch statement to move different values into one numeric label.  
So I have two numeric labels, one of them is “height” and the other is “snubber” and both of them are double. I want the snubber to be 1 if I have height = 10, 2 if 20\<height\<=30, and 3 if height\>=40.

In order to do that I am binding the data value of the snubber to an expression and in there I am using a switch statement to switch between different conditions. Based on the value of height, the data has to be shown on the snubber label, however, I am not able to do that and I am getting syntax errors. I cannot find a lot of information regarding scripting with Switch. I would appreciate any knowledge you could share with me.

I do have the code as well, but since I am sure that is 100% Incorrect I probably better not sharing it 😃

Thank you!

---

<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:** [July 14, 2021, 8:07pm UTC](https://forum.inductiveautomation.com/t/using-switch-statement-to-move-different-values-into-a-numeric-label/48852/2 "2021-07-14T20:07:43Z")

</div>

It isn’t a script. It is an expression function. Do share your code.

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [July 14, 2021, 8:11pm UTC](https://forum.inductiveautomation.com/t/using-switch-statement-to-move-different-values-into-a-numeric-label/48852/3 "2021-07-14T20:11:02Z")

</div>

Then that is why probably not working.  
I wanted to use event handler at the beginning but then I have to use property change for that which I am not sure if it is possible in a way that I have set up my tags or not. The value of my height is binded to an SQL query.

This is the code, which I am sure is not correct!!!  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/1/d118371409381a6845bde359150965ee660260ee.png)

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [July 14, 2021, 8:21pm UTC](https://forum.inductiveautomation.com/t/using-switch-statement-to-move-different-values-into-a-numeric-label/48852/4 "2021-07-14T20:21:26Z")

</div>

The other thing I could do I guess is binding the snubber value to a property (height value) and then use the property change event handler.

In this case, whenever the height value changes, I can write a script that can send a value to snubber based on the height value. I wrote this as an example, but again it is not working.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/0/10f7d7a016d9a61dabd04a9cead15ee480f195ea.png)

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [July 14, 2021, 9:15pm UTC](https://forum.inductiveautomation.com/t/using-switch-statement-to-move-different-values-into-a-numeric-label/48852/5 "2021-07-14T21:15:35Z")

</div>

You can use a nested if expression. Set your snubber label to expression binding and use this

```auto
if({Root Container.height.value} >= 40, 3,
  if({Root Container.height.value} <= 30 && {Root Container.height.value} > 20, 2,
    if({Root Container.height.value} = 10, 1, 
      0
    )
  )
)

```

However, this has some holes in it. What do you want when 10 \< value \< 20, when 30 \< value \< 40, or value \< 10? In the expression I provided, the value will be 0 in those cases, as that is the default I put in.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [July 14, 2021, 9:19pm UTC](https://forum.inductiveautomation.com/t/using-switch-statement-to-move-different-values-into-a-numeric-label/48852/6 "2021-07-14T21:19:43Z")

</div>

Looks like @dkhayes117 has you covered with nested ifs (although I liked to keep all ifs at the same root indenting)  
E.g.

```auto
if({Root Container.height.value} >= 40, 3,
if({Root Container.height.value} <= 30 && {Root Container.height.value} > 20, 2,
if({Root Container.height.value} = 10, 1, 
0
)))

```

I thought I’d mention however for the future, if you do need to use a switch statement for something else, the `case` function is far better imo. It’s far more readable.

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [July 15, 2021, 12:01am UTC](https://forum.inductiveautomation.com/t/using-switch-statement-to-move-different-values-into-a-numeric-label/48852/7 "2021-07-15T00:01:56Z")

</div>

Thank you very very much! I do really appreciate your help :))

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [July 15, 2021, 12:08am UTC](https://forum.inductiveautomation.com/t/using-switch-statement-to-move-different-values-into-a-numeric-label/48852/8 "2021-07-15T00:08:58Z")

</div>

Thank you very much for the information.  
So if I want to use case function, in my own case would be something like this, right?  
case(  
{Root Container.height.value},  
{Root Container.height.value} \>= 40,  
3,  
{Root Container.height.value} \<= 30 && {Root Container.height.value} \> 20,  
2,  
{Root Container.height.value} = 10,  
1,  
0)

Do I need to write this “{Root Container.height.value}” in each line?

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [July 15, 2021, 12:11am UTC](https://forum.inductiveautomation.com/t/using-switch-statement-to-move-different-values-into-a-numeric-label/48852/9 "2021-07-15T00:11:36Z")

</div>

Nope, you can’t use the switch or case statements for your use-case (pun intended). You can only use these functions for discrete values:

```auto
case({Root Container.height.value}
    ,40, 3
    ,41, 4
    ,51, 5
    ,-1
)

```

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [July 15, 2021, 12:24am UTC](https://forum.inductiveautomation.com/t/using-switch-statement-to-move-different-values-into-a-numeric-label/48852/10 "2021-07-15T00:24:04Z")

</div>

I see, great to know!  
Thanks for sharing your information.

---

<div class="post-metadata">

**Author:** ![mcgheeiv](https://avatars.discourse-cdn.com/v4/letter/m/71c47a/32.png) [@mcgheeiv](https://forum.inductiveautomation.com/u/mcgheeiv)\
**Post date:** [July 17, 2021, 10:33am UTC](https://forum.inductiveautomation.com/t/using-switch-statement-to-move-different-values-into-a-numeric-label/48852/11 "2021-07-17T10:33:09Z")

</div>

The following might work for the use-case.

```auto
case(True
    , {Root Container.height.value} >= 40, 3
    , {Root Container.height.value} <= 30 && {Root Container.height.value} > 20, 2
    , {Root Container.height.value} = 10, 1
    , 0)

```

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [July 19, 2021, 12:05pm UTC](https://forum.inductiveautomation.com/t/using-switch-statement-to-move-different-values-into-a-numeric-label/48852/12 "2021-07-19T12:05:38Z")

</div>

Thank you!  
Appreciate it.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [July 19, 2021, 12:12pm UTC](https://forum.inductiveautomation.com/t/using-switch-statement-to-move-different-values-into-a-numeric-label/48852/13 "2021-07-19T12:12:51Z")

</div>

Neat @mcgheeiv, I never thought of using case like that.

---

<div class="post-metadata">

**Author:** ![Attila\_Lazok](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@Attila\_Lazok](https://forum.inductiveautomation.com/u/Attila_Lazok)\
**Post date:** [June 13, 2023, 4:21pm UTC](https://forum.inductiveautomation.com/t/using-switch-statement-to-move-different-values-into-a-numeric-label/48852/14 "2023-06-13T16:21:25Z")

</div>

Thank you for this example! It's the simplest most elegant solution I have seen for solving problems like mine. Easy to read and understand!
