NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

Select Case problem with expressions

Solved!
Go to solution

Hi,

 

I am using Teststand 4.0 and am having a frustrating problem with Select Case statements.

I have attached a sequence which shows the problem.

 

 A loop goes from 0 to 10.

The select case statement should decide which of 3 case statements matches the expression and then pops up a message box.

 

However the simple program does not perform as expected.

 

The first time it enters with a value of 0, it goes to the >7 case statement.

The second time it enters the loop with a value of 1 it goes correctly to the  ❤️ case statement

For the remaining values from 2 to 10 it enters no case statement.

 

Is this a bug in TS 4 ?, or am I doing something wrong ?

 

Thanks,

Mike

 

 

0 Kudos
Message 1 of 6
(8,126 Views)

The cases should be values, not binary comparisons.  For example, you should put "1" if you want the case to execute when Locals.NewVal is 1, not "Locals.New_Val ==1".

 

What is happening is it already is comparing Locals.NewVal, since it is in the Select case.  It tries to find a case where Locals.NewVal is the same as one of the "Value to Compare" expressions in the cases.  When Locals.NewVal is 0, "Locals.New_Val < 3" evaluates to "true".  0 is not equal to true, so this case fails.  The second case is tested next.  "Locals.New_Val > 7" evaluates to "false".  0 is also equal to false, so this case executes!


On the second iteration,  Locals.New_Val < 3 evalutes to true still, but Locals.New_Val is also equal to true since it is 1, so the first case executes.


It sounds like you should be using an If/Else If/Else structure instead of a Select statement.

 

Allen P.

NI

 

0 Kudos
Message 2 of 6
(8,120 Views)

Hi AllenP,

 

Thanks for the reply. I made that small program to try and figure out what was going out in a much larger program where I'm indexing an array and getting weird results. I guess that is why. I understand that putting the Locals.New_Val into the expression is incorrect but the logic of the way Teststand deals with case statements seems all wrong to me.

 

The logic does not make sense to me in where you say,

                   "When Locals.NewVal is 0, "Locals.New_Val < 3" evaluates to "true".  0 is not equal to true, so this case fails."

I would have thought that when the case is true the sub code in that case would execute regardless. The value itself should be inconsequential after the expression was evaluated as true. I rarely use case statements and this is probably why.

 

I seem to recall in C code, usually a constant is preferred in case statements. Also in C if you want a range of values a colon can be used to seperate. Does Teststand support this ?, this would be a solution for me.

 

Thanks again.

Mike

0 Kudos
Message 3 of 6
(8,102 Views)

Hi Mike,

 

Thanks for your post and I hope your well.

 

Allen is correct, in your case it would be easier to handle this with if-else statements.

 

Unlike LabVIEW, Teststand doesn t allow you to execute a single case for mutiple values.

 

For example, if I entered 5, 6, 10, 20 in the case selector label of a case, this case would execute if the value was 5, 6, 10 or 20. However it appears that TestStand's Case steps don't allow this functionality.

 

Case steps in TestStand are first evaluated as an entire expression and then the value returned from the expression is compared.

 

For example, if 5 || 6 || 10 || 20 was entered in the Case step, the code would evaluate as 1. Then TestStand would compare 1 to the value entered in the Select step. If commas are used, TestStand uses the first value of the comma list. For example, if 5, 6, 10, 20 was entered in the Case step, TestStand would evaluate this code as 5. 

 

In order to allow a single case to be executed for multiple values, you need to construct a nested conditional statement. This nested conditional statement will ensure that the case expression evaluates as the correct value.

 

Hope this helps, 

Kind Regards
James Hillman
Applications Engineer 2008 to 2009 National Instruments UK & Ireland
Loughborough University UK - 2006 to 2011
Remember Kudos those who help! 😉
0 Kudos
Message 4 of 6
(8,085 Views)
Solution
Accepted by mike_lan

The problem is you are comparing the Locals.New_Val property to the "Locals.New_Val < 3".  If those two properties match, then it will execute.  If you wanted that particular case to execute when Locals.New_Val is 0, you would just type "0".


Unfortunately there is no way to have multiple cases for a single block of code.

 

In C, you could do it like this:

 

switch(newval)

{

 case 0:

 case 1:

 case 2:

  //do something

  break;

 

case 3:

case 4:

case 5:

case 6:

case 7:

   //do something else

break;

 //etc...

 

}

 

Even in C, this is clunky, and you would probably do the following instead:

 

if (newval < 3)

  // do something

else if (newval < 7)

 // do something else

else 

 // do another thing

 

 The reason why it works in TestStand is that expressions will try to convert different types automatically if it can do so.  false is logically equivalent to 0, and true is logically equivalent to 1, so what you are really comparing to in your case statement is your property (Locals.New_Val) to a boolean expression, that evaluates to either 0 or 1.  It certainly is not obvious when looking at it, but this at least explains why it is happening.  Another way you could do this is to change the Select step's "Item To Compare" to be "true", which will case each case to compare the expression to the value "true".  I still recommend an if/else if/else structure for this kind of problem since it is more straightforward to read, but it is possible to do what you want in this case with a select/case statement.

 

Allen P.

NI

Message 5 of 6
(8,080 Views)

I think I understand it now a bit more thoroughly, so thanks to you both, I'm sure it will help other people too.

 

I have used if, else statements as suggested and all is working fine now and I will try to avoid using case statements in future as they don't work as I expect them to and it will save a lot of head scratching.

I had not realised they were not as intuitive as they seem in comparison with an If / else statement but it certainly clears up the confusion.

 

Many thanks for all you help.

 

regards,

Mike 

 

0 Kudos
Message 6 of 6
(8,071 Views)