# Looping Through Tags

**URL:** <https://forum.inductiveautomation.com/t/looping-through-tags/1826>\
**Category:** Ignition\
**Created:** [May 8, 2010, 7:27pm UTC](https://forum.inductiveautomation.com/t/looping-through-tags/1826 "2010-05-08T19:27:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![robin.abdul](https://avatars.discourse-cdn.com/v4/letter/r/41988e/32.png) [@robin.abdul](https://forum.inductiveautomation.com/u/robin.abdul)\
**Post date:** [May 8, 2010, 7:27pm UTC](https://forum.inductiveautomation.com/t/looping-through-tags/1826/1 "2010-05-08T19:27:33Z")

</div>

Dear Sir,

I am new to Jython Scripting / Ignition Scripting

I have about 16 Tags as memory tags  
and about 16 Tags in the PLC (throught kepware OPC)  
all are boolean tags  
Tag in memory: MOTOR\_SETTINGS/STAND1/EN\_STAND1 (stand 1 to stand 16)  
Tag in plc: MOTOR\_SETTINGS/EN\_STAND1 (stand 1 to stand 16)

I want to dynamically loop through these tags??  
does this works???

x = 1

while x \< 17:  
z = system.tag.getTagValue(“MOTOR\_SETTINGS/STAND%d” % x"/EN\_STAND%d" % x).  
system.tag.writeToTag(“MOTOR\_SETTINGS/EN\_STAND%d” % x, z)  
print x  
x = x+1

could you please adivse me???  
thanks

abdul

---

<div class="post-metadata">

**Author:** ![Step7](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@Step7](https://forum.inductiveautomation.com/u/Step7)\
**Post date:** [May 9, 2010, 5:10am UTC](https://forum.inductiveautomation.com/t/looping-through-tags/1826/2 "2010-05-09T05:10:05Z")

</div>

You’re on the right track, but avoid the “while” instruction when other instructions will do. I have done a few fairly complex projects, and the only place I use “while” is in a socket module. If you forget to program an escape in a “while” loop, you’ll kick yourself. 🙂

So, use the “for” instruction instead. Here is a basic “for” loop with some formatting, and you can apply it to your tag code:

```auto
for i in range(1,17):
    print "MOTOR_SETTINGS/STAND%d/EN_STAND%d" %(i,i)
```

To make it even more dynamic, you could pass in the number of motors if you have that value somewhere:

for i in range(1,NumberOfMotors+1):

“Range” basically loops from n to n-1, which is why I added 1 to the number of motors.
