# Nested loops? why?

**URL:** https://forum.kirupa.com/t/nested-loops-why/4731
**Category:** Uncategorized
**Created:** [August 17, 2002, 9:12pm UTC](https://forum.kirupa.com/t/nested-loops-why/4731 "2002-08-17T21:12:02Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![iceman](https://avatars.discourse-cdn.com/v4/letter/i/46a35a/32.png) [@iceman](https://forum.kirupa.com/u/iceman)
#### Post date: [August 17, 2002, 9:12pm UTC](https://forum.kirupa.com/t/nested-loops-why/4731/1 "2002-08-17T21:12:02Z")

</div>

If possible could someone help explain to me why nested loops must be used. I have this code that I took from the _Training from the source_ book:

v = 0;  
i = -1;  
while(++i \< 2){  
j = -1;  
while(++j \< 2){  
++v;  
name = “picture” + v;  
\_root[picToDuplicate].duplicateMovieClip(name, v);  
\_root[name].\_x = xStart + i \* xSpacing;  
\_root[name].\_y = yStart + j \* ySpacing;  
\_root[name].gotoAndStop(v);  
}  
}  
What I don’t understand is why I have to have this to create two columns couldn’t I just change the while(++i \< 2){ to  
while(++i \< 4){ instead. If someone could help explain why nested loops need to be used and what the purpose is in this situation it would be greatly apprieciated.

Thanks Alot

Kyle:)

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 18, 2002, 7:10pm UTC](https://forum.kirupa.com/t/nested-loops-why/4731/2 "2002-08-18T19:10:46Z")

</div>

because, i and j being used to set the \_x and\_y of the photos, this nested loop will place them in 4 different places; just test it with the values:  
1st loop i=0 /j=0 and then j=1  
2nd loop i=1 /j=0 then j=1

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 18, 2002, 8:07pm UTC](https://forum.kirupa.com/t/nested-loops-why/4731/3 "2002-08-18T20:07:15Z")

</div>

Couldn’t you just make _i_ count to 4 rather then 2, because isn’t that what the code is essentially doing. Just wondering thats why I’m still confused about this code.

Thanks for your help

Kyle:)

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 19, 2002, 3:41am UTC](https://forum.kirupa.com/t/nested-loops-why/4731/4 "2002-08-19T03:41:36Z")

</div>

Think about it for 1 second. Basically, what you want to do is

```auto
while(++i < 2){
  ++v;
  name = "picture" + v;
  _root[picToDuplicate].duplicateMovieClip(name, v);
  _root[name]._x = xStart + i * xSpacing;
}

```

Right? 2 things to notice: all your clips will have the same \_y position  
AND they will have an incrementing \_x position

=\> They are on 1 line only. With the nested loops, you make 2 lines.

pom :asian:
