# No While Loops in Expressions?

**URL:** <https://forum.inductiveautomation.com/t/no-while-loops-in-expressions/87037>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [March 8, 2024, 2:05pm UTC](https://forum.inductiveautomation.com/t/no-while-loops-in-expressions/87037 "2024-03-08T14:05:40Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Estera\_Cioroch](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/estera_cioroch/32/59889_2.png) [@Estera\_Cioroch](https://forum.inductiveautomation.com/u/Estera_Cioroch)\
**Post date:** [March 8, 2024, 2:05pm UTC](https://forum.inductiveautomation.com/t/no-while-loops-in-expressions/87037/1 "2024-03-08T14:05:40Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [March 8, 2024, 2:06pm UTC](https://forum.inductiveautomation.com/t/no-while-loops-in-expressions/87037/2 "2024-03-08T14:06:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 8, 2024, 2:29pm UTC](https://forum.inductiveautomation.com/t/no-while-loops-in-expressions/87037/3 "2024-03-08T14:29:42Z")

</div>

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))`

---

<div class="post-metadata">

**Author:** ![Estera\_Cioroch](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/estera_cioroch/32/59889_2.png) [@Estera\_Cioroch](https://forum.inductiveautomation.com/u/Estera_Cioroch)\
**Post date:** [March 8, 2024, 2:38pm UTC](https://forum.inductiveautomation.com/t/no-while-loops-in-expressions/87037/4 "2024-03-08T14:38:36Z")

</div>

> [@Kevin.Herron](#):
>
> Back up a little and explain what you are trying to accomplish.

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. 🙂

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. 😄

---

<div class="post-metadata">

**Author:** ![crees](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@crees](https://forum.inductiveautomation.com/u/crees)\
**Post date:** [March 8, 2024, 2:45pm UTC](https://forum.inductiveautomation.com/t/no-while-loops-in-expressions/87037/5 "2024-03-08T14:45:57Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 8, 2024, 2:47pm UTC](https://forum.inductiveautomation.com/t/no-while-loops-in-expressions/87037/6 "2024-03-08T14:47:44Z")

</div>

> [@crees](#):
>
> `if({errortag} = True, 2, toFloat({tag}))`

You can (and should) simplify that to:

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

```

---

<div class="post-metadata">

**Author:** ![Estera\_Cioroch](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/estera_cioroch/32/59889_2.png) [@Estera\_Cioroch](https://forum.inductiveautomation.com/u/Estera_Cioroch)\
**Post date:** [March 8, 2024, 2:48pm UTC](https://forum.inductiveautomation.com/t/no-while-loops-in-expressions/87037/7 "2024-03-08T14:48:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![crees](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@crees](https://forum.inductiveautomation.com/u/crees)\
**Post date:** [March 8, 2024, 2:48pm UTC](https://forum.inductiveautomation.com/t/no-while-loops-in-expressions/87037/8 "2024-03-08T14:48:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 8, 2024, 2:50pm UTC](https://forum.inductiveautomation.com/t/no-while-loops-in-expressions/87037/9 "2024-03-08T14:50:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 8, 2024, 3:04pm UTC](https://forum.inductiveautomation.com/t/no-while-loops-in-expressions/87037/10 "2024-03-08T15:04:50Z")

</div>

Side note, totally not applicable to **this** solution:

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

> [@Automation Professionals' Integration Toolkit Module](https://forum.inductiveautomation.com/t/automation-professionals-integration-toolkit-module/74934):
>
> Automation Professionals is pleased to announce the immediately availability of a major upgrade to its free Simulation Aids Integration Toolkit module. The new feature justifying the "major upgrade" language is the implementation of eleven thirty-one new expression functions which use iteration to accomplish complex transforms of datasets and lists, to or from other datasets and lists, applying filtering, ordering, and computed new columns along the way. While there are certain operations that…

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

---

<div class="post-metadata">

**Author:** ![msteele](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/msteele/32/77889_2.png) [@msteele](https://forum.inductiveautomation.com/u/msteele)\
**Post date:** [March 8, 2024, 4:08pm UTC](https://forum.inductiveautomation.com/t/no-while-loops-in-expressions/87037/11 "2024-03-08T16:08:54Z")

</div>

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