   c++ syntax
  Hi,I use this code for a loop:do{//code...}while( node[index].plane == node[(index = node[index].next)].plane);I think my intention is obvious:I want to compare the plane value to the plane value of the next node in the list and at the same time store the next index in index.However, the code seems to work like this:node[index].plane == node[index].plane;index = node[index].next;So the condition is always true.What's wrong with this syntax ?Thanks for help!Quak   
  The syntax is correct. Unfortunatly, the behavior is undefined. operator== is nothing more than a function call, and the order of evaluation of arguments in a function call is undefined. In your case, the right side is being evaluated first. The meaning of the left side is different depending on the order of evaluation.The actual behavior is the reverse of what you claimed it was; it increments index then tests a plane with itself.   ;Quote:I think my intention is obvious...I think you're wrong. [grin]Simplify the code:int oldindex;int index = ...;do{   // stuff   oldindex = index;   index = node[index.next];}while( node[oldindex].plane == node[index].plane);Maybe that will work?   ;Quote:Original post by rip-offSimplify the codeexactly. keep code simple and readable. your code looks like an example of premature optimization. years ago writing confusing code in the name of optimization made sense, but modern compilers solved the need for that. see the doom source code for examples of what not to do anymore. [lol]   ;  Thank for your help. The "simple" solution seems to be the only choice even though it looks ugly to me somehow ;)   ;  I would probably do it like so:index = ...;plane = node[index].plane;do{  ...;  index = node[index].next;}while (node[index].plane == plane);Be careful you don't walk off the end of the node array during all this.  Nobody's code is checking for that.In C++ you could probably do a combination of find_if and for_each instead.  There may be something similiar that does it more directly but I can't think of it offhand.   ;  This looks like a for loop to me.  =)index = ...{for (  PlaneType plane = node[index].plane;  node[index].plane == plane;  index = node[index].next) {  ...}}UnPatented DoubleBrace(tm) technology keeps evil non-standard scope of for() loop variables from leaking.;  It could be a for loop, but as mentioned by gumpy macdrunken Quote:...your code looks like an example of ... optimization...(nice edit)A do while is a quicker loop. Time the different loops yourself while,do while and for."...don't walk off the end of the node array.." quite right.   ;  a do while is not actually quicker, it is just meant to be used for a different purpose... anyway, in my experience it is almost always better to use a for loop when you can.  This is mainly because a properly written for loop will not infinitely loop (unless you tell it to) and will not cause an out-of bounds error (unless you mess it up).   ;Quote:a do while is not actually quickerOk 95% of the time it's quicker.   