Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AND/OR (&&/||) logic for multiple condition statements [closed]

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?

like image 542
miguelarcilla Avatar asked Dec 19 '13 11:12

miguelarcilla


People also ask

What is the use of and or?

And/or (sometimes written and or) is an English grammatical conjunction used to indicate that one or more (or even all) of the cases it connects may occur. It is used as an inclusive or (as in logic and mathematics), because saying "or" in spoken language (or writing "or") might be inclusive or exclusive.

Does and/or mean both?

According to the legal commentators, when used together with “and,” the word “or” usually includes “and” and the “and/or” phrase means “either or both of.” Inclusion of the “/” would not have corrected any error, ambiguity or confusion already inherent in the use of the “and” “or” conjunctive-disjunctive.

Does and/or need a slash?

You can take a bike or a car but you wouldn't take both, so there is no excuse for the and/. The slash these days is a shiny toy that everyone wants to play with. This may explain in part why and/or, with its ersatz air of authority, is more popular than ever.


3 Answers

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.

like image 152
Lasse V. Karlsen Avatar answered Oct 03 '22 07:10

Lasse V. Karlsen


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.

like image 40
harold Avatar answered Oct 03 '22 07:10

harold


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.

like image 25
Tigran Avatar answered Oct 03 '22 05:10

Tigran