# Nested loops? why?

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

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

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.

Kyle:)

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

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