so are assignment expressions
I've always quite liked the idiom
int c;
while ((c = getchar ()) != EOF)
process_char (c);
it often saves clumsy code
int c = getchar ();
while (c != EOF)
{
process_char (c);
c = getchar ();
}
I use the idiom myself frequently. I like the fact that it allows me to
write only a single call to getchar(). However, I think it is a clumsy
construct.
There's a need I've felt for a long time, and I just realized that
there's a feature that could be defined which would meet that need. I'm
inventing this idea while I'm writing this, so please be patient with
any lack of elegance.
What I want is to define a condition for a looping construct, and two
separate blocks of code that get executed repeatedly until the condition
is met, but the condition is tested between the two blocks, rather than
before or after a single block. It would be sort-of half-way between a
while() and a do-while(). I'm horrible at naming things - I'll just use
"token_n" to indicate the new as-yet unnamed keywords. The grammar
productions for this new kind of iteration statement would be:
token_1 statement_1 while (condition) statement_2 token_2
token_1 declaration while (condition) statement_3 token_2
The semantics would be to execute statement_1 unconditionally, then test
the condition. If the condition is false, terminate the entire
statement. Otherwise, execute the second statement, followed by the
first statement, and test the condition again; repeat until the
condition is false. If the both statements are compound statements, they
are treated as a single block for purposes of scope rules. If
statement_1 is a compound statement, and statement_2 is not, statement_2
is treated for purposes of scope rules as being inside of statement_1's
block. The break and continue statements work exactly as they do in
other iteration statements.
The second grammar rule is treated as being equivalent to
token_1 {declaration} while (condition) statement_3 token_2
The example you gave above could then be written as
token_1
int c = getchar();
while(c != EOF)
process_char(c);
token_2
I would prefer that to either of the alternatives you gave above. Note,
among other things, that the scope of 'c' is restricted to this new
iteration statement, something that's not possible with the other
alternatives.