How Much Stuff Can You Cram In There?

The for keyword opens the door to looping in C, as well as in other programming languages. When teaching this concept, I keep the for loop format simple. After all there’s a lot of cryptic stuff between those parenthesis. And — brace yourself — there’s room for a lot of other stuff as well.

First comes the basic format, as presented in my C programming books:

for( initialization; exit_condition; repeat_each )

Each of the items in the parentheses are separated by semicolons. These semicolons are the only required parts of the statement; you can omit any of the other items but the semicolons must remain. In fact, eliminating everything results in an endless loop:

for(;;)

I pronounce this statement as “for-ever.”

As a statement, the for loop ends with a semicolon or it can be followed by a block of statements enclosed in braces. Blah, blah, blah, you should know the rest if you program in C. If you’re just learning, buy my book.

Second, once you become comfortable with the format you can shove other things into a for statement. Each item goes in its own part of the statement and is acted upon in the same manner, though multiple items are separated by commas. The commas add to the delightful confusion of the contraption:

for( x=0, y=10; x<10; x++,y-- )

In this loop, both variables x and y are initialized: x to zero and y to ten. Each is separated by a comma. This initialization takes place first.

The loop continues as long as the value of variable x is less than ten.

For each iteration, the value of variable x is incremented and the value of variable y is decremented. Again, each expression is separated by a comma, but both operations take place each time the loop spins.

So what else can you cram in there?

The for keyword description defines each of its three items as expressions. In C, an expression is evaluated TRUE or FALSE. Assignments are evaluated TRUE. Just about anything in C can be an expression, even a function as the following code demonstrates:

2026_08_01-Lesson.c

#include <stdio.h>

void scale(void)
{
    char s;

    for( s='A'; s<='G'; putchar(s),s++ )
        ;
}

int main()
{
    int x;

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

    return 0;
}

The for loop in the main() function executes three expressions for each iteration: The scale() function is called, a printf() statement outputs text, and the value of variable x is incremented.

Within the scale() function, the putchar() function is set inside the for loop.

Here’s the program’s output:

ABCDEFG0 loop
ABCDEFG1 loop
ABCDEFG2 loop
ABCDEFG3 loop
ABCDEFG4 loop
ABCDEFG5 loop
ABCDEFG6 loop
ABCDEFG7 loop
ABCDEFG8 loop
ABCDEFG9 loop

The point of this lesson was to see whether I could stick all statements in a program inside a for loop. What is the limit?

In the sample code, you see the scale() function. This function is required because one of the things you can’t stick inside a for loop statement is another for loop; for is a keyword and its operation isn’t evaluated as an expression. So, you cannot stick a for loop inside a for loop statement, but you can stick a function in there and the function can have a for loop, which is how I worked around this limitation.

Leave a Reply