Reputation:
I started learning C#, when reading the Logical Operators, I found that:
The || operator represents a logical operator and expression 2 is not evaluated if expression 1 is TRUE. I'm curious about this example:
int x = 5, y = 10;
bool result;
result = (x == 3) || (y != 5);
label12.Text = result.ToString();
If we already know that the value of X is 5, then how the result (x==3) could return true?? Because the left hand side operator in this case returns TRUE. But how?? X =5 and It should be X ==5??? What I misunderstood?? Can someone explain me in a few words??
Much appreciated and thanks in advance
Upvotes: 1
Views: 368
Reputation: 4636
Lets break it down...
int x = 5, y = 10;
result = (x == 3) || (y != 5);
(x==3)
is false
(y!=5)
is true
Since you used ||
the first part of the conditional (x==3)
is false so we need to continue evaluating any other conditionals to determine if the overall expression is true or false ... if the first expression were true we could stop there because the entire expression wouldn't change with continued evaluation and the second part of the conditional would be "short-circuited".
In this case the (y!=5)
must be evaluated which returns true and makes the rest of the expression true.
If instead you used &&
to combine your conditionals then if either side is false the entire expression is false.
int x = 5, y = 10;
result = (x == 3) && (y != 5);
Since in this case (x==3)
is false, no other evaluation needs to be done and result would be set to false without evaluating (y!=5)
Upvotes: 1
Reputation: 56698
Short circuit operations means that here
result = (x == 3) || (y != 5);
y != 5
part would not be evaluated if x == 3
is already true. Basically because there is no need for this - we already know that whole expression is true.
If x is 5 however, both expression will be evaluated, because we still do not know whole result after x == 3
.
Update. Maybe the purpose of this would be more clear with this example. Conside the following, of course simplified, code:
var someObj = SomeOperation();
if (someObj != null && someObj.IsActive)
What would happen if SomeOperation()
returned null? If it wasn't for short-circuit evaluation of this logical statement, this code will throw NullReferenceException
. However IsActive
call will not be called if someObj
is null.
Upvotes: 3
Reputation: 216302
Short circuit evaluation means that the compiler stops to evaluate the expression immediately when it could determine the result ot the whole expression
In your case, when the compiler finds the expression x==3
to be false it cannot stop the evaluation because the second part of the expression could be true.
So it is forced to continue the evaluation, it finds the expression y != 5
to be true and the whole expression become FALSE || TRUE
Upvotes: 0
Reputation: 383
(x == 3) should not evaluate to True. (x ==3) evaluates to False, so (y != 5) gets evaluated and that is true. That is why result is True.
Upvotes: 0
Reputation: 2960
It's an or. The left hand side is false, so it evaluates the right hand side, which is true.
Upvotes: 1