The Linux Page

The for() in C, C++, Java, PHP, JavaScript...

As I am working on my as2js compiler, I stumble on a couple of problems with the for() loop parsing because of the in keyword. That made me think and the for() statement was actually a very funny one. Yes! You can actually write something like this and it compiles:

int zero;
for(3;2;1) zero;

As you can see, all 3 entries in the for() statement are valid expressions and therefore the compiler can compile that code. This creates a loop that runs forever, similar to:

for(;;) zero;

but much less clear to read, obviously.

Now that's food for thought, isn't it?

In JavaScript, you have a small difference in case you use the var keyword because in that case the first expression becomes a list of variables instead and thus the syntax is different.