# "For...In" statements

**URL:** <https://forum.kirupa.com/t/for-in-statements/9465>\
**Category:** flash\
**Created:** [December 12, 2002, 11:20am UTC](https://forum.kirupa.com/t/for-in-statements/9465 "2002-12-12T11:20:06Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![alethos](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/alethos/32/117_2.png) [@alethos](https://forum.kirupa.com/u/alethos)\
**Post date:** [December 12, 2002, 11:20am UTC](https://forum.kirupa.com/t/for-in-statements/9465/1 "2002-12-12T11:20:06Z")

</div>

I am having a hard time understanding for…in loops. I have seen David use it in his signature, and I understand what the loops is doing, but only from the context of the rest of the signature. If somebody could explain how the for…in loop works, I’d be greatful. Here’s what I know:

```php

for (proprtyThing in objectThing) {
  trace (proprtyThing);
}

```

My understanding of this loop tells me that if I run it, I’ll get a list of all the properties of the object named objectThing…but this makes no sense to me!!

---

<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:** [December 12, 2002, 11:30am UTC](https://forum.kirupa.com/t/for-in-statements/9465/2 "2002-12-12T11:30:33Z")

</div>

A _for_ loop is similar to a _while_ loop, but with a more compact syntax

**Syntax:**

```php
for (initialization; condition; update){
substatments
}

```

\*\*Example:\*\*_for- **in** _ loop

```php
for (var speed in ball){
trace ("speed of the ball is = " + speed);
}

```

---

<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:** [December 12, 2002, 11:38am UTC](https://forum.kirupa.com/t/for-in-statements/9465/3 "2002-12-12T11:38:22Z")

</div>

But why can’t you just say:

```php

trace("Speed of the ball = " + ball.speed);

```

That seems a lot more compact to me. Am I missing something?

---

<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:** [December 12, 2002, 12:51pm UTC](https://forum.kirupa.com/t/for-in-statements/9465/4 "2002-12-12T12:51:41Z")

</div>

For in allows you to apply a multiple reference to a declared property in nested MC instances and is an absolute godsend for gaming apps

Suppose I have a christmas tree mc that contains 100 fairy lights mcs nested within it and i wan to to tell all of these lights to gotoAndPlay(“twinkle”)

I’d use the syntax

for (lights in \_root.christmasTree) {  
\_root.christmasTree[lights].gotoAndStop(“twinkle”);  
}

The iterant - in this case lights - can be anything you like, spinachAndWatercress, thursday, a variable.

And the best bit is that if you add 600 more lights to your christmas tree it’ll work just the same

---

<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:** [December 12, 2002, 2:16pm UTC](https://forum.kirupa.com/t/for-in-statements/9465/5 "2002-12-12T14:16:30Z")

</div>

I’m sorry guig0 but you’re wrong.

And Al, if you want some more explanations, there’s something in the AS tricks about it.

pom 🙂

---

<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:** [December 12, 2002, 3:07pm UTC](https://forum.kirupa.com/t/for-in-statements/9465/6 "2002-12-12T15:07:02Z")

</div>

WRONG??? Well… probably:)

but, how wrong:q:

---

<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:** [December 12, 2002, 3:42pm UTC](https://forum.kirupa.com/t/for-in-statements/9465/7 "2002-12-12T15:42:22Z")

</div>

> \*Originally posted by Guig0 \*  
> **for (initialization; condition; update){  
> substatments  
> }**

A for in loop uses an iterant to loop through elements in an array. In my example above it was nested MCs but it could have been anything. Also while it does iterate through each sequential object, you don’t have to specify a break clause or initialise and increment a starting variable.

---

<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:** [December 12, 2002, 4:04pm UTC](https://forum.kirupa.com/t/for-in-statements/9465/8 "2002-12-12T16:04:00Z")

</div>

I was replying to the question on same bases:

> \*Originally posted by alethos \*

```php

for (proprtyThing in objectThing) {
  trace (proprtyThing);
}

```

I dont never said that for has only this aplication. Got it?

=)

---

<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:** [December 12, 2002, 4:18pm UTC](https://forum.kirupa.com/t/for-in-statements/9465/9 "2002-12-12T16:18:57Z")

</div>

Ok, Guigo’s For…Next loop is valid, but it’s not what I was looking for. I know how to use a For…Next loop (i.e. for(i=0;i\<5;i++){blah}), but what I was after was the For…In loop.

Jsk, your two explanations have cleared things up a bit, but not fully. First I need some clarification on the tree example. Specifically, I’d like to know what the “light bulb” MC’s are called. From what I see, you’re saying christmasTree is an MC, but then you call the lights by using the tree as an array (christmasTree[0], [1], etc.). If my light bulb MC’s are called “lightBulb1, 2, etc”, then do I substitute “light” in your code with “lightbulb”? Or can I equally well put “i” instead of “light”?

It seems the key to my understanding is figuring out whether the christmasTree[variable] is read as christmasTree[0], [1], etc **OR** read as christmasTree.light1, .light2, etc. (assuming your MC’s are called light1, 2, etc. If you answer this simple question I think I’ll be totally clear. 🙂

---

<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:** [December 12, 2002, 5:41pm UTC](https://forum.kirupa.com/t/for-in-statements/9465/10 "2002-12-12T17:41:20Z")

</div>

Guig0:  
\<hr\>  
Sorry, I should have quoted alethos not you - I was pointing out the similarity of for() and for in() loops.

That said, if you want to reference the property speed which is contained within the object ball then the script

for (var speed in ball){  
trace ("speed of the ball is = " + speed);  
}

\<i\>is\</i\> wrong.

I’m assuming that you’ve created an object (perhaps a movie clip, perhaps something else like an associative array) and you want to reference its properties.

\<b\>Scenario A - it’s an associative array\</b\>

ball={speed:27, rotation:6.998, direction:‘west’}

If you want to get the value for speed you’d use  
trace("speed of the ball is = "+ball.speed);

using your script:

for (var speed in ball){  
trace ("speed of the ball is = " + speed);  
}

will return the trace

speed of the ball is = speed  
speed of the ball is = rotation  
speed of the ball is = direction

and using the corrected script i.e.

for (var speed in ball){  
trace ("speed of the ball is = " + ball[speed]);  
}

will return

speed of the ball is = 27  
speed of the ball is = 6.998  
speed of the ball is = west

The point is that the name of the iterant (in this case speed) is \<b\>completely irrelevant\</b\> and the script

for (var mashedTurnip in ball){  
trace ("speed of the ball is = " + ball[mashedTurnip]);  
}

will return exactly the same result

\<b\>Scenario B - its an object\</b\>

ball = new Object();  
ball.speed=27

if you want to get the value for speed you’d use:

trace("speed of the ball is = "+ball[speed]);

\<b\>Scenario C - it’s a MC\</b\>

trace("speed of the ball is = "+instanceName.speed);

alethos:  
\<hr\>  
The beauty of for in loops is that you don’t have to enumerate each element by instance name. As I said the iterant can be whateve you like and

for (bakedPotatoes in \_root.christmasTree) {  
\_root.christmasTree[bakedPotatoes].gotoAndStop(“twinkle”);  
}

will work just as well. A for in loop iterates through every element - you don’t have to specify them. And (even better) you can use it as a recursive function so if my lights MCs had children called redLight, yellowLight, etc. I could loop through those as well.

---

<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:** [December 12, 2002, 5:48pm UTC](https://forum.kirupa.com/t/for-in-statements/9465/11 "2002-12-12T17:48:37Z")

</div>

Beautifully explained, thank you.

My only comment for others that might be reading this thread is that I thought associative arrays in Flash ARE just instances of the Object() object.

So

ball={speed:27, rotation:6.998, direction:‘west’}

and

ball = new Object();  
ball.speed=27;  
ball.rotation=6.998;  
ball.direction=‘west’;

are the same thing, 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:** [December 12, 2002, 7:32pm UTC](https://forum.kirupa.com/t/for-in-statements/9465/12 "2002-12-12T19:32:45Z")

</div>

[EDIT] now I got it! sorry!

=)

---

<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:** [December 12, 2002, 10:57pm UTC](https://forum.kirupa.com/t/for-in-statements/9465/13 "2002-12-12T22:57:10Z")

</div>

Al\>\> Yes, they’re the same.  
Jsk\>\> That was very well explained indeed 🙂
