September 09, 2003
"for" revisited

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

Here are some comments on "for"

Posted by: sangles on September 9, 2003 08:39 PM

Looks like I should read more blogs instead of esoteric programming books. Thanks for that link!

Posted by: Mario on September 9, 2003 09:01 PM

It's amazing how many 3rd year Computer Science uni undergrads don't even remember that...

I can't count the amount the times I've seen




would be sufficient.

Posted by: Tim Lucas on September 10, 2003 05:07 AM
Post a comment

Email Address:



Remember info?

Thank you!

Most Visited Entries
Sketches, Works & Source Code
In Love with
Powered by
Movable Type 2.661

© Copyright Mario Klingemann

Syndicate this site:
RSS 1.0 - RSS 2.0

Quasimondo @ flickr
Quasimondo @ LinkedIn
Quasimondo @ Twitter
Quasimondo @ Facebook
Quasimondo @ MySpace
Quasimondo is a Bright
Citizen of the TRansnational Republic
My other blog in german

My family name is written Klingemann,
not Klingelmann, Klingeman, Klingaman, Kingemann,
Kindermann, Killingaman, Klingman, Klingmann, Klingonman
Klingemman, Cleangerman, Klingerman or Kleangerman

profile for Quasimondo at Stack Overflow, Q&A for professional and enthusiast programmers