No While Loops in Expressions?

Am I correct in observing that there are no while loops available in expressions? If not, what would be the best way to get around this? Here is the logic I am trying to accomplish

while(!tag) {
     if(errortag)
         return 2.0
     else
         return 0.0
     }
while(tag) {
     if(errortag)
         return 2.0;
     else
         return 1.0
     }

There are no while loops in expressions. Back up a little and explain what you are trying to accomplish.

3 Likes

If I'm reading the flow correctly:
binEnc(tag, errortag)

EDIT: Oops, that will return a three if both are on. Perhaps this, instead...

if(errortag, 2, toInt(tag))

3 Likes

No worries. So first, I am not a seasoned Ignition programmer like many of the whizzes on here (nor do I have a strictly programming background) so just a fair disclaimer. :slight_smile:

But referencing the code above, I am trying to accomplish that logic however it would look like in expression script. I mostly just want to get ideas from users. I started getting on a track of using switch and if statements, a memory tag, etc. but a while loop is kind of what's needed and its a little difficult to get around. My ideas so far have just been needlessly complicated.

I am programming a pump component and a corresponding label to reflect three statuses: stopped, running, and faulted.

Guess this context was missing in the above code but the "tag" variable in my code above is a two-state tag with ON state "Running" (1) and OFF state "Stopped" (0.) And then "errortag" is "Faulted" (2.)

Hopefully that isn't too confusing. :smile:

I think you've explained it quite well. You're just confused on how tags work. They are constantly re-evaluated based on their schedule, so the expression essentially runs in a while loop.

You can use what Jordan posted above, and it would work.

if({errortag} = True, 2, toFloat({tag}))

I put toFloat as you seemed to have 0.0 and 2.0, but Int would work as well.

1 Like

You can (and should) simplify that to:

if({errortag}, 2, toFloat({tag}))

@crees Thanks!! Honestly...its possible. Hence my disclaimer above :slight_smile: I'll try what you and @JordanCClark suggested and see if it works! If not, I'll be back. Thanks for the help!

I only add the "= True" as I've learned the hard way, if {errortag} is null, it breaks the expression.

It would break the expression whether you use = True or not. Don't waste CPU cycles comparing booleans to True. Perspective needs you to conserve CPU cycles wherever you can.

5 Likes

Side note, totally not applicable to this solution:

While Ignition natively provides no looping in pure expressions bindings, it IS possible:

Replacing jython with pure expressions is a huge performance win, and Perspective needs all the help it can get.

8 Likes

Endorsed. These Integration Toolkit iteration expressions have been a gamechanger.

2 Likes