Can't incremen int inside funciton call -- bug?

Ary Manzana ary at esperanto.org.ar
Tue May 29 15:22:02 PDT 2007


Hi.

Shouldn't that be ++treeLevel?

In general var++ is an expression that returns var and leaves var with 
the value var + 1. ++var is an expression that returns var + 1 and 
leaves var with var + 1.

int x = 1;
int y = x++; // leaves y = 1, x = 2

int x = 1;
int y = ++x; // leaves y = 2, x = 2

Further, your two codes are differentes. In the first one you increment 
treeLevel in each foreach iteration, while on the second you increment 
it once before the foreach loop.

Best regards,
Ary

Aldarris escribió:
> Hello.
> 
> I have a nested function that iterates through a tree recursively.
> here is it code:
> 
> void recursiveXMLTreeIteration(XMLNode currentNode, int treeLevel)
> {
> 	writefln(treeLevel);
> 	tabOffset.length = 0;
> 	for(int i = 0; i < treeLevel; i++)
> 		{tabOffset ~= "\t";}
> .......			
> 	foreach(XMLNode childNode; currentNode.getChildrenList())
> 		{recursiveXMLTreeIteration(childNode, treeLevel++);}
> }
> 
> recursiveXMLTreeIteration(this, 0);
> 
> Does not work as expected, in a tree of a root node plus two child nodes it prints 0,0,1.
> 
> It treeLevel++; is moved outside, works as expected:
> .......			
>         treeLevel++;
> 	foreach(XMLNode childNode; currentNode.getChildrenList())
> 		{recursiveXMLTreeIteration(childNode, treeLevel)
> }
> 
> 
> Prints 0,1,1,



More information about the Digitalmars-d mailing list