A Compact for Loop – Solution

I hope you were able to cram a lot of expressions into a for loop statement, which is the challenge for this month’s Exercise. Even if you know this trick, it’s important to understand it’s limitations — which is something I discovered during my research.

Here is my solution, which uses an empty for loop because all the statements that would otherwise roost inside a for loop block are stuffed in the for statement itself:

2023_02-Exercise.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int r,count;

    /* loop until r = 99 */
    for( srand((unsigned)time(NULL)), r=0, count=1 ;
            r!=99 ;
            count++, r=rand()%100, printf("%3d : %2d\n",count,r)
       )
        ;
    printf("%3d : %2d\n",count,r);

    return(0);
}

At Line 10, I split the three parts of a for loop statement into three separate lines. First, the initialization:

srand((unsigned)time(NULL)), r=0, count=1 ;

You might expect to see the srand() function in a statement before the for loop. I stuck it into the looping statement on a whim and — guess what? — it works! Not every compiler allows you to stick a function into the for loop statement in this manner, but clang in the Windows Subsystem for Linux seems to be tolerant. The other two items are variable initialization expressions: r=0, count=1. I tried to declare these variables in the for loop statement, but the compiler threw a hissy fit.

r!=99 ;

The loop repeats as long as the value of r (initialized to zero) isn’t equal to 99. This expression is typical for a for loop termination, nothing fancy.

The final part of the for loop statement contains three expressions that normally would appear in the for loop block:

count++, r=rand()%100, printf("%3d : %2d\n",count,r)

Variable count is incremented. A random value r is fetched in the range zero through 99. A printf() statement outputs the program’s progress. Some compilers may balk at setting an assignment expression (r=rand()%100 into a for loop statement. Even so, the point of this exercise is to condense the loop as much as you can. If the compiler doesn’t allow it, but you tried it anyway, give yourself a C For Dummies bonus point.

The final printf() statement is required to output the value 99 after it’s encountered. Otherwise, the loop ends before this value is output.

I hope your solution met with success and you perhaps learned a bit about how whacky you can get with a for statement. I wouldn’t cram so much stuff into the statement for the programs I write. Yet, doing so is allowed (by some compilers) and a fun way to prove how ridiculously cryptic the C programming language can be.

3 thoughts on “A Compact for Loop – Solution

  1. The extra printf() statement in my solution isn’t necessary for some compilers. It’s a completely unexpected and weird result, but apparently some compilers do output the printf() statement at the loop’s terminating condition. If you see two “99” values in your output, then you too have a compiler that runs that printf() statement before the loop completes. Weird. Weird. Weird.

  2. The explanation for why the printf() statement in a for loop statement executes makes sense: The full for statement must be executed, even when the terminating condition is reached. While the statements within the loop block are then skipped, any remaining statements in the for statement itself are executed.

  3. I don’t think I could eat two 99s.

    I can’t see any reason for the third part of a for loop being executed after the test in the centre part fails. However, people do some really arcane stuff in for loops so I suppose there might be a reason.

Leave a Reply