5

If you have an if-statement in C# that checks multiple conditions:

if (a == 5 && b == 9) { ... }

Does b == 9 still get checked if a == 5 condition is false, or does it automatically exit since there's no way this could pass anymore?

Similarly, for an OR if-statement:

if (a == 5 || b == 9) { ... }

Will b == 9 still get checked if a == 5 is true?

miguelarcilla
  • 1,426
  • 1
  • 20
  • 38
  • 3
    Sorry, but read the [manual](http://msdn.microsoft.com/en-us/library/2a723cdk.aspx). This is a question that can be answered by the documentation and by trying it out for yourself. – Tim Schmelter Dec 19 '13 at 11:53
  • @TimSchmelter Fair enough. I guess I just wasn't sure how to word my question for a search engine – miguelarcilla Dec 19 '13 at 12:01
  • you're asking for the functionality of _operators_. So next time you could include this term in the search, f.e. "C# operator". – Tim Schmelter Dec 19 '13 at 12:04

6 Answers6

25

Both && and || is "short-circuiting" operators, which means that if the answer is known from the left operand, the right operand is not evaluated.

This means that:

a && b

b will not be evaluated if a is false, since the final answer is already known.

Likewise:

a || b

b will not be evaluated if a is true, since the final answer is already known.

If you want both operands to be evaluated, use the & and | operators instead.

The bonus of this is that you can write expressions that would fail if all operands was evaluated. Here's a typical if-statement:

if (a != null && a.SomeProperty != null && a.SomeProperty.Inner != null)
    ... use a.SomeProperty.Inner

If a was null, and the expression would go on to evaluate a.SomeProperty, it would throw a NullReferenceException, but since && short-circuits, if a is null, the expression will not evaluate the rest and thus not throw the exception.

Obviously, if you replace && with &, it will throw that exception if either a or a.SomeProperty is null.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
2

Conceptually, && and || short-circuit.

But since you don't have any side-effects there, the JIT compiler is free to remove the short-circuiting. I don't know whether it actually does so or not.

harold
  • 61,398
  • 6
  • 86
  • 164
1

For : if (a == 5 && b == 9) { ... }

Does b == 9 still get checked if a == 5 condition is false, or does it automatically exit since there's no way this could pass anymore?

If a == 5 is false no any other control will be executed on that line.

For: if (a == 5 || b == 9) { ... }

Will b == 9 still get checked if a == 5 is true?

Pass inside immediately, as first condition already satisfies requirements.

Tigran
  • 61,654
  • 8
  • 86
  • 123
0

Using the AND operator, according to the boolean logic, all the conditions must be evaluated to TRUE. If only one of them isn't satisfied, the result of the conditions will be FALSE.

Does b == 9 still get checked if a == 5 condition is false, or does it automatically exit since there's no way this could pass anymore?

It does automatically exit, because the first condition is false and the result is already known.

For the OR operator, you need that, at least, one of the conditions is TRUE and your logic will be executed. If the first one is not satisfied, the application will check the other conditions.

Will b == 9 still get checked if a == 5 is true?

No, it won't be checked, because the first condition is TRUE and then it's not necessary to check another condition.

Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
0

Short-cirtuiting is defined by standard. Otherwise it would be impossible to say what is the outcome of expression such as:

if (a != null && a.IsValid) { ... }

In some C# compilers it would work fine, in some others it would cause an exception. That's why the standard is there to define common behavior.

EDIT: clarified last statement.

Zoran Horvat
  • 10,924
  • 3
  • 31
  • 43
-1

in AND check a==5 if is true then will go to b==9 else will not go to b==9. in OR: it will check a==5 and b==9.

Nahum
  • 6,959
  • 12
  • 48
  • 69
  • Hi user3119036 welcome to SA. your answer is OK but needs some parsing check what I did. – Nahum Dec 19 '13 at 12:15