if else ...

  • Thread starter Thread starter Hrvoje Voda
  • Start date Start date
Paul,

What you say is true in C, but not in C#. C# requires that conditions
be boolean, which is different from int, so you cannot say

int a = 1;
int b = 2;
if (a & b) ...

The compiler will flag the "if" as an error, because the result of the
expression is int, not bool.
 
Paul,

What you say is true in C, but not in C#. C# requires that conditions
be boolean, which is different from int, so you cannot say
Hi Bruce,

Thanks for this. Did not know this. I am bit of a C# novice really,
having programmed in C and C++ for the best part of last 10 years.

So bitwise operators will still work on bool variables under C#.
Doesn't feel right somehow. Wonder if this is intended by design. I
reckon programmers will start interchanging & and && depending on the
code situation (short circuiting or no). Why not as this is "allowed"?

Time to retune our "C" brains perhaps, and start watching out for & and
&&s.

Cheers,

Paul.
 
Indie Pop said:
You can do it without the or:

If ( x.ToString().Length() + y.ToString().Length > 2 )

Not only is this a bad solution, but it is also WRONG
if x = 9 , y = 10
it should be false but your "Solution" says it is true

You might argue that yours gives the "Correct Answer" for:
if (x >9 or y > 9)

but then what about
x = 5, y = -1

Ooops...wrong again.

And god forbid we need to change it to
if (x >15 or y < 7)

The standard solution is a trivial change
Your Broken solution falls apart completely

How about we stick with the "||"?
There is enough unintelligible code out there as it is.

Bill
 
Back
Top