<story><title>Let’s Build a Web Server. Part 1</title><url>http://ruslanspivak.com/lsbaws-part1/</url></story><parent_chain><item><author>_asummers</author><text>Why is for(;;) used instead of while(true)? I&amp;#x27;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?&lt;p&gt;[0] &lt;a href=&quot;http:&amp;#x2F;&amp;#x2F;stackoverflow.com&amp;#x2F;questions&amp;#x2F;2288856&amp;#x2F;when-implementing-an-infinite-loop-is-there-a-difference-in-using-while1-vs-f&quot; rel=&quot;nofollow&quot;&gt;http:&amp;#x2F;&amp;#x2F;stackoverflow.com&amp;#x2F;questions&amp;#x2F;2288856&amp;#x2F;when-implementing...&lt;/a&gt;</text></item><item><author>corysama</author><text>Pretty much the same minimal http server in C:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    #include &amp;lt;WinSock.h&amp;gt;
    #include &amp;lt;stdio.h&amp;gt;
    #pragma comment(lib, &amp;quot;wsock32.lib&amp;quot;)
    int main(int argc, const char *argv[]) {
        WSADATA wsadata;
        WSAStartup(2, &amp;amp;wsadata);
        sockaddr_in address;
        memset(&amp;amp;address, 0, sizeof(address));
        address.sin_family      = AF_INET;
        address.sin_addr.s_addr = inet_addr(&amp;quot;0.0.0.0&amp;quot;);
        address.sin_port        = htons(80);
        int sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
        bind(sock, (struct sockaddr *)&amp;amp;address, sizeof(address));
        for(;;) {
            listen(sock, 0);
            int connection = accept(sock, NULL, NULL);
            char recvBuffer[1024];
            int recvSize = recv(connection, recvBuffer, sizeof(recvBuffer)-1, 0);
            recvBuffer[recvSize]=0;
            printf(recvBuffer);
            char response[] = &amp;quot;HTTP&amp;#x2F;1.1 200 OK\nContent-Type: text&amp;#x2F;html\n\nlol&amp;quot;;
            send(connection, response, sizeof(response), 0);
            closesocket(connection);
        }
        return 0;
    }&lt;/code&gt;&lt;/pre&gt;</text></item></parent_chain><comment><author>corysama</author><text>They are equivalent.  It&amp;#x27;s just a matter of personal preference.  I like it because it&amp;#x27;s shorter and it doesn&amp;#x27;t read as a loop that evaluates the value &amp;#x27;true&amp;#x27; on each iteration.  Even though you know the evaluation optimizes away trivially if you think about it, I don&amp;#x27;t even want to think that far when I&amp;#x27;m scanning code.</text></comment>
<story><title>Let’s Build a Web Server. Part 1</title><url>http://ruslanspivak.com/lsbaws-part1/</url></story><parent_chain><item><author>_asummers</author><text>Why is for(;;) used instead of while(true)? I&amp;#x27;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?&lt;p&gt;[0] &lt;a href=&quot;http:&amp;#x2F;&amp;#x2F;stackoverflow.com&amp;#x2F;questions&amp;#x2F;2288856&amp;#x2F;when-implementing-an-infinite-loop-is-there-a-difference-in-using-while1-vs-f&quot; rel=&quot;nofollow&quot;&gt;http:&amp;#x2F;&amp;#x2F;stackoverflow.com&amp;#x2F;questions&amp;#x2F;2288856&amp;#x2F;when-implementing...&lt;/a&gt;</text></item><item><author>corysama</author><text>Pretty much the same minimal http server in C:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    #include &amp;lt;WinSock.h&amp;gt;
    #include &amp;lt;stdio.h&amp;gt;
    #pragma comment(lib, &amp;quot;wsock32.lib&amp;quot;)
    int main(int argc, const char *argv[]) {
        WSADATA wsadata;
        WSAStartup(2, &amp;amp;wsadata);
        sockaddr_in address;
        memset(&amp;amp;address, 0, sizeof(address));
        address.sin_family      = AF_INET;
        address.sin_addr.s_addr = inet_addr(&amp;quot;0.0.0.0&amp;quot;);
        address.sin_port        = htons(80);
        int sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
        bind(sock, (struct sockaddr *)&amp;amp;address, sizeof(address));
        for(;;) {
            listen(sock, 0);
            int connection = accept(sock, NULL, NULL);
            char recvBuffer[1024];
            int recvSize = recv(connection, recvBuffer, sizeof(recvBuffer)-1, 0);
            recvBuffer[recvSize]=0;
            printf(recvBuffer);
            char response[] = &amp;quot;HTTP&amp;#x2F;1.1 200 OK\nContent-Type: text&amp;#x2F;html\n\nlol&amp;quot;;
            send(connection, response, sizeof(response), 0);
            closesocket(connection);
        }
        return 0;
    }&lt;/code&gt;&lt;/pre&gt;</text></item></parent_chain><comment><author>nchelluri</author><text>I had a professor who gave some reasoning about this. He&amp;#x27;s written a paper: &lt;a href=&quot;http:&amp;#x2F;&amp;#x2F;plg.uwaterloo.ca&amp;#x2F;~pabuhr&amp;#x2F;papers&amp;#x2F;MELoop.ps&quot; rel=&quot;nofollow&quot;&gt;http:&amp;#x2F;&amp;#x2F;plg.uwaterloo.ca&amp;#x2F;~pabuhr&amp;#x2F;papers&amp;#x2F;MELoop.ps&lt;/a&gt;&lt;p&gt;I haven&amp;#x27;t read the whole thing but I like this bit:&lt;p&gt;&amp;gt; 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:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  read(a, b, c)
  WHILE NOT eof DO
    { process a, b, &amp;amp; c }
    read(a, b, c)
  END WHILE


  LOOP
    read(a, b, c)
    WHEN eof EXIT
    { process a, b, &amp;amp; c }
  END LOOP
&lt;/code&gt;&lt;/pre&gt;
&amp;gt; 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.</text></comment>