The Heretofore Loop

(Editor’s note: this one goes out to Benji, who thinks that programming is equivalent to stringing together a bunch of words like “if for else”.)

The heretofore loop is a programming language construct that was originally part of the C99 revision of the C programming language. It is the counterpart to the for loop, just as the do-while loop is the counterpart to the while loop.

Advocates of the heretofore loop argued that the alternate style of loop completed the looping features of the language and provided enhanced flexibility when writing code. Opponents countered that the do-while loop is rarely used in place of the while loop, so there was little reason to add another looping construct that could be expected to play a similarly negligible role.

The heretofore loop is similar to the for loop, as the looping mechanism is composed of three parts: an initialization statement, a condition statement, and a statement executed after each successful iteration (often called the “increment” statement). The difference is that the three parts occur at the end of the loop rather than the beginning, thus guaranteeing that the loop body will be executed at least once regardless of the condition.

Syntax:

here
{
    body;
} for (initialization; condition; increment);

The name heretofore comes from the fact that the code from here to for is always executed at least once.

Example #1:

int socket;
char buffer[256];
ssize_t totalRead;
 
/* assume socket has been connected successfully */
 
here
{
    ssize_t bytesRead = recv(socket, buffer, sizeof(buffer), 0);
} for (totalRead = 0; bytesRead > 0; totalRead += bytesRead);
 
printf("Read %d bytes from the socket\n", totalRead);

This example reveals one of the finer points of the heretofore loop’s semantics. The increment statement is only executed if the condition evaluates to a non-zero value. In the above code, if recv returns -1 because of an error, the condition bytesRead > 0 will evaluate to 0, and so totalRead += bytesRead will not execute.

The example also reveals one of the benefits of the heretofore loop: fencepost errors can often be avoided by eliminating the need to repeat code such as the call to recv both inside and outside the loop, or place it inside the conditional of a while loop.

Example #2:

int i = 42;
here
{
    printf("%d\n", i);
} for (i = 0; i < 5; i++);

This example illustrates another pitfall of the heretofore loop. The above code prints the numbers 42, 1, 2, 3, 4, and 5. The first iteration uses the value of i from outside the loop. At the end of the iteration, i is set to 0, but then incremented to 1 after the conditional evaluates to 1. Thus, the value 0 for i is never seen inside the loop body. Similarly, i does take on the value 5 inside the loop. This is very different behavior compared to the seemingly similar code;

int i = 42;
for (i = 0; i < 5; i++)
{
    printf("%d\n", i);
}

Which prints 0, 1, 2, 3, and 4.

Needless to say, the subtle but significant differences in semantics between the heretofore loop and the more conventional for loop resulted in many of its early supporters to disfavor its inclusion in C99 as the standardization process reached its conclusion.

6 Responses

  1. I’m curious… how did you come across this? Maybe only CS456-ish people can appreciate this, but definitely a neat idea. I don’t assume you read the history of C99?

  2. I *knew* I forgot to mention something in the post:

    DISCLAIMER: The above post is packed full of lies and is based entirely on an in-joke between my non-CS roommate and I.

  3. I do not believe that computer programming is the equivalent of stringing words like “if for else” together. I believe that it is the equivalent of stringing words like “if for else” together while saying x = 42. Or for my yellow pig friends, I believe you pick a random number, like seventeen for x to equal.
    Anyway I’ve got to go now. I’ve got a busy day of main int voiding things. Plus I have to study for a test since I don’t want to get a C++ in the class. I should stay up all night to study for this exam. I’ll probably drink some java to do that. I also need to come up with a gift idea for a friend. I was thinking a perl necklace, any other ideas? I also need to work out so I can keep referring to my muscles as pythons. Wow, I’ve been rambling for quite some time, Maya how time flies.

  4. I feel used

  5. Benji: The underlying pattern behind your puns is pretty basic. You may want to come up with a different scheme. Or at least come up with ones subtle enough that you need to C# to pick them out. But maybe that’s just too much of a Haskel. Nevertheless, you’re always welcome to come forth and add some more. No pressure (or 0 pascal, as they say in physics). Would you like to continue making smalltalk, or should we move on?

  6. Both of those felt scripted, floating in the middle of the void.

Comments are closed.