News for hackers. Stuff that matters.
Re:Let’s Build a Web Server. Part 1 by _asummers on Friday April 03, 2015 @08:25PM (#9319364)
Why is for(;;) used instead of while(true)? I've seen this idiom used many times but never understood the reasoning why. According to this[0] SO, there are no performance gains, so why would one prefer the for version?

[0] http://stackoverflow.com/questions/2288856/when-implementing...

Re:Let’s Build a Web Server. Part 1 by corysama on Friday April 03, 2015 @08:41PM (#9319409)
They are equivalent. It's just a matter of personal preference. I like it because it's shorter and it doesn't read as a loop that evaluates the value 'true' on each iteration. Even though you know the evaluation optimizes away trivially if you think about it, I don't even want to think that far when I'm scanning code.
Re:Let’s Build a Web Server. Part 1 by nchelluri on Friday April 03, 2015 @08:42PM (#9319413)
I had a professor who gave some reasoning about this. He's written a paper: http://plg.uwaterloo.ca/~pabuhr/papers/MELoop.ps

I haven't read the whole thing but I like this bit:

> There are many algorithms that are best written using an exit from the middle of a loop. If not written in this fashion, techniques such as the traditional “priming” of a loop must be used. For example, in reading a file:

  read(a, b, c)
  WHILE NOT eof DO
    { process a, b, & c }
    read(a, b, c)
  END WHILE


  LOOP
    read(a, b, c)
    WHEN eof EXIT
    { process a, b, & c }
  END LOOP
> The example on the left [top] shows the priming of a loop. This requires duplication of the READ statement or, in other cases, creation of a subprogram to eliminate the duplication, both of which are undesirable. As well, loop priming can be done an arbitrary distance before the beginning of the loop making the program difficult to read as the priming statement(s) is critical to the understanding of the loop.
Re:Let’s Build a Web Server. Part 1 by _asummers on Friday April 03, 2015 @09:02PM (#9319458)
This wasn't really my question. I was asking about the different infinite loop idioms in C family languages and the rationale for using one over the other.
Re:Let’s Build a Web Server. Part 1 by ptaipale on Saturday April 04, 2015 @02:02AM (#9320088)
Because the original K&R book introduced for(;;) structure.
Re:Let’s Build a Web Server. Part 1 by ternaryoperator on Friday April 03, 2015 @08:54PM (#9319438)
In the first versions of C, 'true' was not a keyword, so for(;;) was the way an endless loop was encoded. After that, it became the convention.
Re:Let’s Build a Web Server. Part 1 by machosx on Saturday April 04, 2015 @12:16AM (#9319879)
Why not just use while(1)?
Re:Let’s Build a Web Server. Part 1 by ufo on Saturday April 04, 2015 @01:05AM (#9319971)
Personal preference. Some people think "for(;;)" looks nicer.
Re:Let’s Build a Web Server. Part 1 by z92 on Saturday April 04, 2015 @04:52AM (#9320388)
the "for" version is also shorter by a single byte.
Re:Let’s Build a Web Server. Part 1 by pcthrowaway on Saturday April 04, 2015 @02:19AM (#9320126)
I just started learning Go last night, but I'll hazard a guess: since Go draws heavily from C, but has the for loop as its only looping construct, it seems likely that this habit has carried back into C code written by developers who've used Go, or who've been reading a lot C code written by developers who also use Go.
Re:Let’s Build a Web Server. Part 1 by jsprogrammer on Saturday April 04, 2015 @03:17AM (#9320249)
for(;;) existed way before Go came along.
Re:Let’s Build a Web Server. Part 1 by pcthrowaway on Saturday April 04, 2015 @04:48AM (#9320380)
I'm not saying it didn't, but 'while (true)' would be the more intuitive way to write that in any language besides Go.
Re:Let’s Build a Web Server. Part 1 by throwawayfor on Saturday April 04, 2015 @10:34AM (#9320924)
Another reason is that MSVC whines about loop conditionals evaluating to constants. Or at least it used to. So while(1) would cause a warning, but not for(;;)