For Ever

The most basic and perhaps ancient of programming techniques is the loop. And the most basic loop is the for loop. It’s also pretty flexible.

All loops have three major parts:

  • The setup
  • The counter
  • The terminating event

Each of these parts can operate independently, all together, or neglected completely. In a for loop, they work like this:

for(setup,terminating_event,counter)

The other piece of the loop is the repeating statement or group of statements. The counter and terminating event can take place in those statements as well. In fact, the setup can take place before the loop even starts. So in C you’re effectively left with the following structure:

for(;;)

I read that as “for ever,” as it defines an endless loop. Observe that even though elements are missing, the semicolons are still required. And if you don’t want the loop to spin forever, you simply code the three conditions elsewhere:

#include <stdio.h>

int main()
{
    int x=1;                   /* the setup */

    for(;;)
    {
        printf("%d\n",x++);    /* x++ is the counter */
        if(x>10)               /* the exit condition */
            break;
    }
    return(0);
}

On the other side of pulling everything out of the for statement is the concept of cramming everything into it. Consider the following code:

#include <stdio.h>

int main()
{
    int x;

    for(x=1;x<=10;printf("%d\n",x++))
        ;
    return(0);
}

Above, all the action takes place at Line 7. The printf() statement is okay as the counter, effectively a statement within a statement. After all, isn’t x++ a statement?

The output from the two code samples is the same:

1
2
3
4
5
6
7
8
9
10

The difference between the examples is simply the approach. I would argue that neither of the examples are as readable as doing the loop the traditional way:

for(x=1;x<=10;x++)
    printf("%d\n",x);

That's pretty much what most coders expect. In fact, I don't know of any advantage to deliberately writing the code any other way, other than it's funky and non-traditional.

Speaking of non-traditional, you can use commas to pack even more items into the for statement:

#include <stdio.h>

int main()
{
    int x,y;

    for(x=1,y=20;x<10;x++,y--)
        printf("%d\t%d\n",x,y);

    return(0);
}

In Line 7, the first and last parts of the for statement have two items, separated by a comma. The terminating event (middle item) doesn't need two conditions as the compiler would ignore the one on the left.

What you see in this example is effectively two loops working in one statement: x counts up from 1 through 10 and y counts down from 20 to 11. Here's the output:

1	20
2	19
3	18
4	17
5	16
6	15
7	14
8	13
9	12
10	11

I don't know of a limit on the number of items you can place in a for statement. Also, you don't have to pair up the items. For example, I've written code where the first part of the for statement had two items but last part had only one.

Perhaps this quirk remains one of the trivial parts of the C language. It's something I'd recommend using with caution as not every programmer recognizes it, and loading up a for statement does tend to obfuscate your code.

Leave a Reply