Switching integer value ranges with case statements?


#1

Hi, I’m trying to do something like this:

switch X
case (x < 0) do…
case (x in range 0…5) do…
case (x > 5) do…
default do…
endswitch

Regardless what I’ve tried, it seems always to hit the default case, as none of the prior experimental cases test true. I guess I could bodge together a bunch of IF’s however I switch where-ever I can as it’s much cleaner. Can anyone shed light on if what I’m trying to do is possible at all and if so what am I doing wrong?

Thanks
/Max


#2

what type of variable do you have x set to. If it is anything other than integer then it will likely fail and drop into your default. When comparing a variable with a number you need to make sure that you have the variable set to integer or decimal (if you want to compare with a decimal number)


#3

Hi Rizlah, thanks - it’s indeed an integer as indicated in subject. However it might be the logic is messed up for the range. I’m presuming the case evaluates true/false on the following statement in round brackets or compares with a numerical value.

If the above is correct, the first and last case statements should work, right? Yet they don’t.
Also I was hoping to be able to specify something like ((x >= 0) && (x <= 5)) - not entirely sure all the brackets are necessary, yet has anyone got something like this to work?

Thanks,
Max


#4

You should post a sample piston so we have something to go by.


#5

Exactly what I was going to say.

Post a green screen shot of your code and this will prevent misunderstandings


#6

I am not sure if this will be helpful, but when I use cases, I use them for very specific results…

If my query is vague like less than, greater than, or is between, those are the times I use IF conditions. Either way, the piston has to process (do the math) to determine if it is true or not…


#7

@WCmore that’s the conclusion I’ve reached as well. The logic should be pretty clear to anyone, yet cases doesn’t seem to work unless you use specific values.