NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

How to implement "if/then/else" or "case" logic in a Statement Expression?

I need a loop in my test sequence, so I define a Locals.LoopIndex which is of type Number, and I also define a Locals.PromptMessage, which is of type String. Within the loop body, have a statement step, in which I want to assign Locals.PromptMessage different strings according to the value of Locals.LoopIndex. The logic is like this (in suedo code)

case Locals.LoopIndex:
1: Locals.PromptMessage = "string1";
break;
2: Locals.PromptMessage = "string2";
break;
...

or

if (Locals.LoopIndex == 1)
then Locals.PromptMessage = "string1";
else if (Locals.LoopIndex == 2)
then Locals.PromptMessage = "string2";
...

How can I implement this in the Statement Expression?

Thanks!
0 Kudos
Message 1 of 7
(13,379 Views)
You can use the conditional expression (not sure what it is called). Syntax is Locals.variable=(BooleanExpression?ValueIfTrue:ValueIfFalse). To implement your pseudo code it would look like this:
Locals.PromptMessage=(Locals.LoopIndex==1?"string1":"string2")
This means the same as:
If Locals.LoopIndex=1 then
Locals.PromptMessage = "string1"
Else
Locals.PromptMessage = "string2"
End If

Another way to do it is like this, but the first method is preferred:
Locals.LoopIndex==1?(Locals.PromptMessage="string1"):(Locals.PromptMessage="string2") -- must use parenthesis as shown
This syntax is BooleanExpression?(Statement if BooleanExpression is true):(Statement if BooleanExpression is false)

You can nest the expression to create ElseIf conditions:
Locals.PromptMessage=(Locals.LoopIndex==1?"string1":(Locals.LoopIndex==2?"string2":"string3"))
This means the same as:
If Locals.LoopIndex=1 then
Locals.PromptMessage = "string1"
ElseIf Locals.LoopIndex=2 then
Locals.PromptMessage = "string2"
Else
Locals.PromptMessage = "string3"
End If

I don't know if there is a limit to the nesting.
Hope this helps.
- tbob

Inventor of the WORM Global
Message 2 of 7
(13,370 Views)
I don't know what happend but the frowny faces replaced the colon in my previous reply. Please substitute a colon for the faces.
- tbob

Inventor of the WORM Global
0 Kudos
Message 3 of 7
(13,368 Views)
Sorry, the frowny faces replaced a colon followed by an open parenthesis. I guess this forum uses the colon for special icons.
Testing : ( - colon, space, open parenthesis
😞 - same without the space.
How about 🙂 - colon, closed parenthesis.
- tbob

Inventor of the WORM Global
0 Kudos
Message 4 of 7
(13,365 Views)
I tried the nested way - I nested it 16 times, and it works! Thanks a lot!

By the way, the faces testing are interesting 🙂

Thanks again,
Eileen
0 Kudos
Message 5 of 7
(13,359 Views)

Note that if you need to use multiple expression statements for each condition, the following syntax is acceptable and more readable:

Locals.LoopIndex==1? //IF

(

Locals.PromptMessage="string1",

expression2,

expression3,

😞  //ELSE

(

Locals.PromptMessage="string2",

expression4,

espression5,

)

Message 6 of 7
(10,663 Views)