3 Answers
3

No. The for(i=0;i<10;i++) is a classic programming construct (see Traditional for loops) that is present in many languages. It can be broken down to:

start-expression; end-condition; end-of-iteration-expression

In other words, what I wrote above means "initialize i to 0 and, while i is less than 10, do something and then increment i by 1. Yes the syntax is confusing but that's just the way it is. The end-of-iteration-expression (++x in this case) is executed once at the end of each loop. It is equivalent to writing:

while(i<10){print i; ++i}

As for the $x, I believe that just checks that a field of that number exists and that its contents do not evaluate to false (as explained in Mathias's answer below). $N will return true if the field number N exists and is not a type of false. For example:

@Hastur because there will always be a $5. So, every time the loop runs, it checks whether $5 exists. Because it does, the loop will be executed. This will go on until there is no $5 which will never happen because your input has at least 5 fields. The second prints nothing because the condition fails immediately: there is no $5.
–
terdon♦Aug 8 '14 at 14:31

The first part is not a condition. It's more like start-expression; end-condition; end-of-iteration-expression (note that the end-of-iteration-expression doesn't have to be an increment).
–
Stéphane ChazelasAug 8 '14 at 14:40

1

You should add i=0; before while(i<10){print i; ++i}, otherwise it's not equivalent and just UB.
–
gurkaAug 8 '14 at 19:04

A more correct awk program would be {for(x=1;$x"";++x)print $x}. This works because it fixes the type of the test expression as a string, rather than a numeric string, and a string is false only if it is empty. With the default FS, an empty field is possible only at the end of the fields.
–
riciAug 8 '14 at 19:26

@rici, it means your program wouldn't stop when it meets 0 in a line, right?
–
ZenAug 9 '14 at 2:56

@rici, I've tested it, it avoid the 0 problem. It's quite important in this issue, I think you really should write a supplement answer for this point. Great point.
–
ZenAug 9 '14 at 3:00

++x and x++ are functionally equivalent when used stand-alone.
As discussed in the Stack Overflow question that you referenced,

There may be performance (i.e., timing) differences.

The results of something = ++x;
and something = x++; are different –
but your example isn’t doing that.

So, as far as the incrementing of x is concerned,
your example is equivalent to

awk '{for(x=1;$x;x++) print $x}'

The standalone $x is equivalent to $x != "",
so the loop will iterate until it encounters a blank field.
This is a lazy shortcut for x <= NF,
where NF is the number of fields in the current record (line).
For the purposes of your example, this is harmless, AFAICT.
But, if you specify a non-default field separator,

awk -F, '{for(x=1;$x;++x) print $x}'

this will try to do the same as your example, but splitting lines at commas.
If you type a b, it will output a b.
If you type a,b, it will print a and b on separate lines.
But if you input a,,b, it will output a and then stop, because $2 is null.