Need to understand for loops and i math

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!

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.



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.

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.

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]

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.

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


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

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

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?

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”