# Need to understand for loops and i math

**URL:** <https://forum.kirupa.com/t/need-to-understand-for-loops-and-i-math/261950>\
**Category:** flash\
**Created:** [June 2, 2008, 3:27am UTC](https://forum.kirupa.com/t/need-to-understand-for-loops-and-i-math/261950 "2008-06-02T03:27:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![setzer9999](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@setzer9999](https://forum.kirupa.com/u/setzer9999)\
**Post date:** [June 2, 2008, 3:27am UTC](https://forum.kirupa.com/t/need-to-understand-for-loops-and-i-math/261950/1 "2008-06-02T03:27:46Z")

</div>

I have this code:

> 

var x:XML = new XML();  
x.ignoreWhite = true;

var urls:Array = new Array();  
var captions:Array = new Array();  
var whatday:Number;

x.onLoad = function() {  
var photos:Array = this.firstChild.childNodes;  
for(i=0;i\<photos.length;i++) {  
urls.push(photos\*.attributes.url);  
captions.push(photos\*.attributes.caption);  
}  
holder.loadMovie(urls[0]);  
caption.text = captions[0];  
}

x.load(“january2009.xml”);

day1.onPress = function(){  
holder.loadMovie(urls[0]);  
caption.text = captions[0];  
}

day1.onPress = function(){  
holder.loadMovie(urls[1]);  
caption.text = captions[1];  
}

which I have taken off of another learning site online.

I am trying to edit the code, but every time I do, I get strange, or no, results.

I understand all of the code, believe it or not, except for this part:

> 

x.onLoad = function() {  
var photos:Array = this.firstChild.childNodes;  
for(i=0;i\<photos.length;i++) {  
urls.push(photos\*.attributes.url);  
captions.push(photos\*.attributes.caption);  
}

I don’t know what a “for” loop does

I don’t understand what the lower case “i” is referring to.

I don’t know what push is or what the entire line of code there means.

I am trying to create an xml menu where there are more buttons than just a previous and next (1, 2, 3, 4) buttons and each button loads the appropriate node content. Help!

---

<div class="post-metadata">

**Author:** ![mngBJ](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@mngBJ](https://forum.kirupa.com/u/mngBJ)\
**Post date:** [June 2, 2008, 4:28am UTC](https://forum.kirupa.com/t/need-to-understand-for-loops-and-i-math/261950/2 "2008-06-02T04:28:44Z")

</div>

try this one. but i’m not sure 100% work this one. cuz i didn’t see the XML file. and one suggestion change name of variable x, cuz x is flash’s variable.

```auto

var x:XML = new XML();
x.ignoreWhite = true;

var urls:Array = new Array();
var captions:Array = new Array();
var whatday:Number;

x.onLoad = function(success) {
	if (success) {
		var rootNode = this.firstChild;
		for (var aNode:XMLNode = rootNode.firstChild; aNode != null; aNode=aNode.nextSibling) {
			urls.push(aNode.attributes.url);
			captions.push(aNode.attributes.caption);
		}

		holder.loadMovie(urls[0]);
		caption.text = captions[0];
		day1.onPress = function() {
			holder.loadMovie(urls[0]);
			caption.text = captions[0];
		};

		day1.onPress = function() {
			holder.loadMovie(urls[1]);
			caption.text = captions[1];
		};
	} else {
		trace("XML not loaded");
	}
};

x.load("january2009.xml");

```

and sorry about my english.

---

<div class="post-metadata">

**Author:** ![setzer9999](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@setzer9999](https://forum.kirupa.com/u/setzer9999)\
**Post date:** [June 2, 2008, 4:44am UTC](https://forum.kirupa.com/t/need-to-understand-for-loops-and-i-math/261950/3 "2008-06-02T04:44:05Z")

</div>

It works great. thank you. I wish I understood what this code was doing. If anyone would care to give me a small lesson in this that would be awesome.

---

<div class="post-metadata">

**Author:** ![small\_guy](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@small\_guy](https://forum.kirupa.com/u/small_guy)\
**Post date:** [June 2, 2008, 4:44am UTC](https://forum.kirupa.com/t/need-to-understand-for-loops-and-i-math/261950/4 "2008-06-02T04:44:49Z")

</div>

The for statement sets up conditions that are continually being checked for (hence why it is also called a For loop since it continues to “loop” through the statement until the conditions are met.

It is referencing your Array. You want to start with the first item in your Array. Arrays are 0 indexed based. Thus if you have say 5 items in your array, the first item’s position in the array is not 1 but rather 0. (Thus with 5 items in your array your last item would actually be the 4th item in the array since the first item is 0.)

In your For loop (or For statement) there are 3 parameters. The first one is the initialization expression that sets the starting value. If you want to begin with the first item in your array (which is 0, as just exaplained above) then in the for loop the initial value of i = 0.

The second expression in the for loop is the conditional expression that is being tested for each time the loop is running.

The final expression is the updating expression. i++ means that it is increasing the value of i by 1. (incrementing). You could also decrease the value of i by 1 (1–) or decrement.

pushing is, in essence, having it move to the next item in the Array.

[QUOTE=setzer9999;2334550]I have this code:

which I have taken off of another learning site online.

I am trying to edit the code, but every time I do, I get strange, or no, results.

I understand all of the code, believe it or not, except for this part:

I don’t know what a “for” loop does

I don’t understand what the lower case “i” is referring to.

I don’t know what push is or what the entire line of code there means.

I am trying to create an xml menu where there are more buttons than just a previous and next (1, 2, 3, 4) buttons and each button loads the appropriate node content. Help![/QUOTE]

---

<div class="post-metadata">

**Author:** ![setzer9999](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@setzer9999](https://forum.kirupa.com/u/setzer9999)\
**Post date:** [June 2, 2008, 5:09am UTC](https://forum.kirupa.com/t/need-to-understand-for-loops-and-i-math/261950/5 "2008-06-02T05:09:46Z")

</div>

Thank you. Now I actually clearly understand what a for loop is.

I still don’t know what “i” is as in i++

What is “i” referring to exactly?

I’ve looked everywhere in the flash documentation about this and can’t find it, I only see “i” being used a lot but never explained anywhere.

---

<div class="post-metadata">

**Author:** ![pixel\_streamer](https://avatars.discourse-cdn.com/v4/letter/p/a8b319/32.png) [@pixel\_streamer](https://forum.kirupa.com/u/pixel_streamer)\
**Post date:** [June 2, 2008, 5:49am UTC](https://forum.kirupa.com/t/need-to-understand-for-loops-and-i-math/261950/6 "2008-06-02T05:49:01Z")

</div>

“i” is a variable that stands in for “increment.” It’s a variable pulled completely out of thin air.

You may see a for loop with this syntax sometimes, too:

```auto

for (var i:Number=0; i<10;i++){
trace ("something"+i);
}
trace ("--------------");

```

## In the above case, i is a number. The code above literally traces: something0 something1 something2 something3 something4 something5 something6 something7 something8 something9

---

<div class="post-metadata">

**Author:** ![setzer9999](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@setzer9999](https://forum.kirupa.com/u/setzer9999)\
**Post date:** [June 2, 2008, 5:56am UTC](https://forum.kirupa.com/t/need-to-understand-for-loops-and-i-math/261950/7 "2008-06-02T05:56:49Z")

</div>

I figured out how to add another text field instead if you read what I had here before. Problem solved.

---

<div class="post-metadata">

**Author:** ![setzer9999](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@setzer9999](https://forum.kirupa.com/u/setzer9999)\
**Post date:** [June 2, 2008, 6:01am UTC](https://forum.kirupa.com/t/need-to-understand-for-loops-and-i-math/261950/8 "2008-06-02T06:01:20Z")

</div>

I don’t want to double post, but I need to thank pixel streamer for his response as well, and also have a follow up.

I see that in your example, you identified the variable type for i like so…

var i:Number…

What is i if you don’t define it as a specific kind of variable? As in…

i=0 or i++

When it is not in a declared variable type?

---

<div class="post-metadata">

**Author:** ![randomagain](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@randomagain](https://forum.kirupa.com/u/randomagain)\
**Post date:** [June 2, 2008, 9:47am UTC](https://forum.kirupa.com/t/need-to-understand-for-loops-and-i-math/261950/9 "2008-06-02T09:47:15Z")

</div>

Its good protocal. It can stop memory bleeding and can apparantly speed up you application.

put basically, if you don’t declare a variable but use the correct syntax, when you export a swf it does it for you.

note you can use other letters, “j” “k” this helps to stop numbers getting mixed up if you have more complext for loops, but “i” is the standard for “illiteration”
