# For loop with xml - cant understand the loop

**URL:** <https://forum.kirupa.com/t/for-loop-with-xml-cant-understand-the-loop/174779>\
**Category:** Uncategorized\
**Created:** [January 9, 2006, 10:29am UTC](https://forum.kirupa.com/t/for-loop-with-xml-cant-understand-the-loop/174779 "2006-01-09T10:29:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![skjc](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@skjc](https://forum.kirupa.com/u/skjc)\
**Post date:** [January 9, 2006, 10:29am UTC](https://forum.kirupa.com/t/for-loop-with-xml-cant-understand-the-loop/174779/1 "2006-01-09T10:29:32Z")

</div>

i have tried to keep this as simple as possible in order to demonstrate my confusion - i have looked at this for too long now and i really dont understand how this for loop works - sorry for the example length.

_xml code:_  
\<?xml version=“1.0” encoding=“iso-8859-1”?\>  
\<root\>  
\<node1\>First node value\</node1\>  
\</root\>

_action script (ignoring the xml load part) :_

function showNodes(node) {  
trace("NODE = " + node);  
for(var child = node.firstChild; child != null; child = child.nextSibling) {  
trace("Node First Child = " + node.firstChild);  
trace("child next sibling = " + child.nextSibling);  
showNodes(child);  
}  
trace("xxxxxxxxxxxxxxxxxxxxxxxx " + node.nodeName + " " + node.nodeValue + " " + node.nodeType);  
trace("yyyyyyyyyyyyyyyyyyyyyyyy " + child);  
}

_output:_

NODE = \<?xml version=“1.0” encoding=“iso-8859-1”?\>\<root\>\<node1\>First node value\</node1\>\</root\>  
Node First Child = \<root\>\<node1\>First node value\</node1\>\</root\>  
child next sibling = null  
NODE = \<root\>\<node1\>First node value\</node1\>\</root\>  
Node First Child = \<node1\>First node value\</node1\>  
child next sibling = null  
NODE = \<node1\>First node value\</node1\>  
Node First Child = First node value  
child next sibling = null  
NODE = First node value  
xxxxxxxxxxxxxxxxxxxxxxxx null First node value 3  
yyyyyyyyyyyyyyyyyyyyyyyy null  
xxxxxxxxxxxxxxxxxxxxxxxx node1 null 1  
yyyyyyyyyyyyyyyyyyyyyyyy null  
xxxxxxxxxxxxxxxxxxxxxxxx root null 1  
yyyyyyyyyyyyyyyyyyyyyyyy null  
xxxxxxxxxxxxxxxxxxxxxxxx null null 1  
yyyyyyyyyyyyyyyyyyyyyyyy null

i can fully see how things work up until i hit the FOR LOOP EVALUATION:

child !=null

at that point the loop is broken and the trace statements with xxxxx and yyyy come into play but why is there 4 lots of x and y traces?

if the loop is broken and child = null which it does why does the statement carry on 4 more times - should it not just return?

i understand that the update part of the for loop will execute only at the end of the loop but this still does not show why it should carry on like this?

seriously grateful for any help as its driving me nuts

thank you very much in advance
