# While loop + dupMC confusion

**URL:** <https://forum.kirupa.com/t/while-loop-dupmc-confusion/29717>\
**Category:** Uncategorized\
**Created:** [August 29, 2003, 5:26pm UTC](https://forum.kirupa.com/t/while-loop-dupmc-confusion/29717 "2003-08-29T17:26:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremy](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jeremy](https://forum.kirupa.com/u/jeremy)\
**Post date:** [August 29, 2003, 5:26pm UTC](https://forum.kirupa.com/t/while-loop-dupmc-confusion/29717/1 "2003-08-29T17:26:12Z")

</div>

maybe I haven’t had enough coffee yet, but this is throwing me for a loop (bad pun :P).

My confusion lies with the “myname” var. If I declare it outside of the loop, the loop does not work correctly, but if I declare it inside the loop it works fine. I guess I don’t get why “mycount” is incremented, and works, being declared outside of the loop, but “myname” does not get incremented unless its inside the loop. Is this just the way dupMC works?

[AS]amount=0;  
mycount = 1;  
xmoveNum = 30;  
ymoveNum = 0;

while (amount\<3) {  
myname = “MC”+mycount; // \<-here  
duplicateMovieClip(myMC, myname, mycount);  
setProperty (myname, \_x, xmove);  
setProperty (myname, \_y, ymove);  
mycount++;  
amount++;  
xmove += xmoveNum;  
ymove += ymoveNum;  
}  
[/AS]

---

<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 29, 2003, 5:44pm UTC](https://forum.kirupa.com/t/while-loop-dupmc-confusion/29717/2 "2003-08-29T17:44:17Z")

</div>

Think logically. This is executed everytime until amount is equal as or higher than 3. And, it adds 1 to mycount every time. Now have a look at the line:

myname = “MC”+mycount;

It will do this also three times, but with mycount being different every time, since we add 1 every time. It will also set it’s values every time:

setProperty (myname, \_x, xmove);  
setProperty (myname, \_y, ymove);

So if you’d place that code (  
myname = “MC”+mycount;  
setProperty (myname, \_x, xmove);  
setProperty (myname, \_y, ymove);  
) outside the loop, it will only do this once. Do you understand ?

---

<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 29, 2003, 7:03pm UTC](https://forum.kirupa.com/t/while-loop-dupmc-confusion/29717/3 "2003-08-29T19:03:50Z")

</div>

I understand what the code does, I am just confused about the scope of the two variables myname and mycount.

I guess I don’t understand why the increments to mycount, aren’t reflected in myname when declaring it outside the loop, (see “A” below). Why on the second loop is myname still MC1?

```auto
amount=0;
mycount = 1;
xmoveNum = 30;
ymoveNum = 0;
myname = "MC"+mycount; // <-here "A"

while (amount<3) {
        myname = "MC"+mycount; // <-or here "B"
        duplicateMovieClip(myMC, myname, mycount);
        setProperty (myname, _x, xmove);
        setProperty (myname, _y, ymove);
        mycount++;
        amount++;
        xmove += xmoveNum;
        ymove += ymoveNum;
}

```

---

<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 29, 2003, 7:56pm UTC](https://forum.kirupa.com/t/while-loop-dupmc-confusion/29717/4 "2003-08-29T19:56:09Z")

</div>

> _Originally posted by jeremy_ \*  
> **I guess I don’t understand why the increments to mycount, aren’t reflected in myname when declaring it outside the loop**

Because mycount is incremented inside the loop, and you used “MC”+mycount before the loop.

[AS]  
amount=0;  
mycount = 1;  
xmoveNum = 30;  
ymoveNum = 0;  
myname = “MC”+mycount; // \<-here “A”  
//At this point, mycount is 1 so myname will be “MC1” here.

while (amount\<3) {  
myname = “MC”+mycount; // \<-or here “B”  
//First time: mycount is still 1. So myname will be “MC1”  
//Second time: mycount is 2. So myname will be “MC2”  
//Third time: mycount is 3. So myname will be “MC3”  
duplicateMovieClip(myMC, myname, mycount);  
setProperty (myname, \_x, xmove);  
setProperty (myname, \_y, ymove);  
mycount++;  
//First time: mycount is now 2  
//Second time: mycount is now 3  
//Third time: mycount is now 4  
amount++;  
//First time: amount is now 1  
//Second time: amount is now 2  
//Third time: amount is now 3, so not longer \<3 so the loop will stop.  
xmove += xmoveNum;  
ymove += ymoveNum;  
}

//At this point, myname would be “MC4”  
[/AS]

---

<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 29, 2003, 10:46pm UTC](https://forum.kirupa.com/t/while-loop-dupmc-confusion/29717/5 "2003-08-29T22:46:04Z")

</div>

I appreciate your help on this, thank you!.

So once a loop is “looping” it no longer pulls values from outside the loop, it just stores the values initially set in the loop’s first pass, and then edits those? Is that right?

---

<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 30, 2003, 6:16am UTC](https://forum.kirupa.com/t/while-loop-dupmc-confusion/29717/6 "2003-08-30T06:16:44Z")

</div>

Well I don’t know if the loop keeps a copy, edits that one, and palces it back, (that is what you meant right ?) but I can tell you it can edit values from outside the loop. Place this code on the first frame of a new file, and test it, you’ll see:

[AS]  
mycount=0  
trace("Mycount before: "+mycount)  
while(mycount\<3){  
mycount++  
trace("Mycount in the loop: "+mycount)  
}  
trace("Mycount after the loop: "+mycount)  
[/AS]
