"James Curran" <Ja*********@mv ps.org> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
"J. Jones" <jj@networld.co m> wrote in message
news:41******** *******@networl d.com...
A lot of C-based languages allow variable assignment in an if statement
(one = instead of two), which is a failure of the language.
For if()s I'll agree. But for while() statements, it's a very useful
and valuable construct. It allows you to write things like:
I throughly disagree, especially when you are working with error codes
T x;
if ((x = getvalue()) == 0)
{
print("ERROR!") ;
}
with pretty much the same argument you had for while, the other method is as
bad in some peoples eyes as this is to others.
Also, from a pure design standpoint, modifying if to not support assignments
is simply not going to happen. C languages are expression based, if takes an
expression. Assignment cannot be changed into a non-expression withough
removing the functionality of while and a number of other things.
Also, changing if alone would not do any good, it would simply save things
in one particular cirucmstance.
consider that
if ( 1 == (y = (( x =10) + 5 )))
{
}
and
if (1 == (y == ((x = 10) + 5)))
{
}
or
bool b = v = true;
and
bool b = v == true;
exhibits the same mistake, even though the error occurs within a grouped
expression, it is still a logic error with the same root cause...someone
used the wrong operator.
Another cute one is that disallowing assingments removes the capacity to do
if (x == y = 5)
{
}
without extraneous which, again, is a completely valid expression.
Illadvised, perhaps, but valid and consistent.
The only real way to fix this is to change the assignment or comparison
operator so that they are not so similar or to throw expression based
semantics totally out the window. I imagine that both would be unattractive
to most people who use C based languages, as that would cost us our beloved
++ and -- operators.