Using Switch Statement to move different values into a numeric label

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

Thank you!

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

1 Like

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!!!

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.

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

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, 

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.


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

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, 

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.


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

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?
{Root Container.height.value},
{Root Container.height.value} >= 40,
{Root Container.height.value} <= 30 && {Root Container.height.value} > 20,
{Root Container.height.value} = 10,

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

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:

case({Root Container.height.value}
    ,40, 3
    ,41, 4
    ,51, 5
1 Like

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

The following might work for the use-case.

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

Thank you!
Appreciate it.

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

1 Like