# For loop execution issue - bug?

**URL:** <https://forum.inductiveautomation.com/t/for-loop-execution-issue-bug/43490>\
**Category:** Ignition\
**Tags:** perspective, scripting, python\
**Created:** [February 16, 2021, 11:18pm UTC](https://forum.inductiveautomation.com/t/for-loop-execution-issue-bug/43490 "2021-02-16T23:18:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![iceberg](https://avatars.discourse-cdn.com/v4/letter/i/e99b99/32.png) [@iceberg](https://forum.inductiveautomation.com/u/iceberg)\
**Post date:** [February 16, 2021, 11:18pm UTC](https://forum.inductiveautomation.com/t/for-loop-execution-issue-bug/43490/1 "2021-02-16T23:18:41Z")

</div>

I have a for loop that has an if statement nested in it inside of a script. The for loop does not appear to be executing correctly, as the for loop iterates before trying to enter the if loop. This is in a script in a binding in Perspective.

 ![python_code](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/c/ccc49a111c97ef0ae26204d7622609a39d3e1430.jpeg) ![console](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/9/6919b9e14c14902fa89721fa5c8c983b94293454.jpeg)

I have attached my code, and the console output.

I am currently using **Ignition Platform** `8.0.12` and **Perspective Module** `1.0.12`.

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [February 16, 2021, 11:38pm UTC](https://forum.inductiveautomation.com/t/for-loop-execution-issue-bug/43490/2 "2021-02-16T23:38:51Z")

</div>

Your if statements are not going to work as I’m guessing you intended.

If you want to test a value against 2 or more conditions you would have to check them all explicitly like this.

```auto
if x == 2 or x == 3 or x == 4:
    # Do something

```

Your 1st if statement evaluates if dataData[rows][1] is equal to ‘10015531’ and then it evaluates your next item (‘10023468’) as a statement to see if it can be evaluated as True or not and does that for all of the remaining items.

In your case you may be able to do something like this to make it a little cleaner. I left out some of the conditions.

```auto
if dateData[rows][1] in ['10015531', '10023468', '10036077', '10026408', '10010805', '10033990', '10010888']:
    # Do Something

```

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [February 16, 2021, 11:42pm UTC](https://forum.inductiveautomation.com/t/for-loop-execution-issue-bug/43490/3 "2021-02-16T23:42:10Z")

</div>

What @JGJohnson said.

```auto
'x' == 'y' or 'z'

```

is a conditional evaluation which will result in `True` in Python because ‘z’ is not `False` or `None`. The Pythonic way to determine if a given value is within a set of values is to use

```auto
x in someList

```

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [February 17, 2021, 4:36am UTC](https://forum.inductiveautomation.com/t/for-loop-execution-issue-bug/43490/4 "2021-02-17T04:36:02Z")

</div>

In addition to what @JGJohnson and @cmallonee said, you’re not taking full advantage of the pyDataSet model (assuming dateData is a pyDataSet

Your loop should look something like this

```auto
build = (‘10015531’,’10023468’)
warehouse = (‘10015539’,’10036724’)

for row in dateData:
    if row[1] in build:
        scheduledBuildDate = row[2]
        actualBuildDate = row[3]
    elif row[1] in warehouse:
        scheduledWarehouseDate = row[2]
        actualWarehouseDate = row[3]

```

To make it even more readable you can use the column name

So say that the header for column1 is Code, column 2 is Scheduled, and column 3 is Actual. Then your code would read.

```auto
build = (‘10015531’,’10023468’)
warehouse = (‘10015539’,’10036724’)

for row in dateData:
    if row[‘Code’] in build:
        scheduledBuildDate = row[‘Scheduled’]
        actualBuildDate = row[‘Actual’]
    elif row[‘Code’] in warehouse:
        scheduledWarehouseDate = row[‘Scheduled’]
        actualWarehouseDate = row[‘Actual’]

```
