Sometimes I browse through Robert Sedgewick's "Algorithms In C" for inspiration. Recently I checked out the Graph Algorithms - more for the beauty of the adjacent graphics than because of their usefulness. Reading the code for Boruvka's Minimum Spanning Tree algorithm I stumbled across the following line:
for (UFinit(F->V);E!=0;E=N){...}
Whew - as I have not studied informatics this was somehow a surprise for me. This is not the usual for (i=0;i<10;i++) thing. This is just a different way to use the "for" command opposed to the "established" way. So as a reminder here is what "for" is actually doing:
for ([execute this part at the beginning];[if this part returns true execute the following part],[execute this part if the middle part returned true])
So there is no rule that says that you have to use a counter in the "for" command. You can also do other things:
for (var a=["THREE","TWO","ONE"];a.length>0;trace(a.pop())){}
for (someObj.init();someObj.ready();someObj.doSomething()){...}
And another relevation occured to me: The use of the comma "," operator. It was made for "for" loops:
for (i=10,j=0;i--,j++;i>0){...}
Yes, you can have multiple loops going on at the same time. And where could I have read this first? In Colin Moock's ASDG - just look under "Comma Operator"...
I hope that this "relevation" doesn't make me look like a complete fool - be honest did you ever think about using such a construction?
Posted at September 09, 2003 06:51 PM | Further reading




