C# for loop and comma operator

  • Thread starter Thread starter PIEBALD
  • Start date Start date
P

PIEBALD

What is the reason behind disallowing the comma operator in the for-condition
of a for statement?
It's allowed in C.
It's allowed in the for-initializer and the for-iterator, but not the
for-condition; that seems arbitrary.
 
What is the reason behind disallowing the comma operator in the
for-condition
of a for statement?
It's allowed in C.
It's allowed in the for-initializer and the for-iterator, but not the
for-condition; that seems arbitrary.

Because it would be confusing and useless.

Neither the initializer nor the iterator are dependent on the expression
value, so having comma-separated items doesn't matter. But there's no
context-obvious solution for which expression in a list of comma-separated
items should be the _actual_ conditional used for the condition (you have
to remember the arbitrary and forgettable rule), nor is there actually any
practical reason to allow it (the only reason to even use that "feature"
would be to put something with side-effects in there, which is just asking
for a maintenance headache).

For what it's worth, I'm not even sure it's always allowed in C. I don't
have a specification handy, but the MSDN documentation for Microsoft's
compiler says that commas are allowed only in the initializer and
iterator, not the condition. The Xcode compiler on my Mac, which is based
on gcc, allows it. So it may be one of those many things in the C/C++
specification that is implementation-dependent.

Anyway, the bottom line is that I can see lots of good reasons for C# to
_not_ allow that, and no good reason for it to be allowed.

Pete
 
Peter Duniho said:
But there's no
context-obvious solution for which expression in a list of comma-separated
items should be the _actual_ conditional used for the condition

I understood it to always be the last, which is the rule in C.
(you have
to remember the arbitrary and forgettable rule)

Never heard of it.

I agree that it's generally not a good idea to do, but it seems that so few
people know about the comma operator that I doubt it would get abused by
newbies.

I've been trying to contrive a situation to use it.
 
I understood it to always be the last, which is the rule in C.


Never heard of it.

Of course you have. You just stated it. My point is that there's nothing
about the syntax that is suggestive of the rule. It's entirely arbitrary
(it could just as easily have been the first expression, not the last),
and easy to forget.
I agree that it's generally not a good idea to do, but it seems that so
few
people know about the comma operator that I doubt it would get abused by
newbies.

I've been trying to contrive a situation to use it.

Exactly. There's no good reason to use it, it can only make the code
harder to read, and so it was left out.

Pete
 
Exactly. There's no good reason to use it, it can only make the code
harder to read, and so it was left out.

After reading the spec (ECMA) I see now that C# has no "comma opeator" at all.

It has statement-expression-list instead, which provides the same thing, but
isn't valid in a for-condition:

"
statement-expression:
invocation-expression
object-creation-expression
assignment
post-increment-expression
post-decrement-expression
pre-increment-expression
pre-decrement-expression

statement-expression-list:
statement-expression
statement-expression-list , statement-expression
"
 
Back
Top