Reputation: 5282
I was wondering what happens when a program processes an if-structure with multiple conditions. I have an idea, but I'm not sure about it. I'll give an example:
List<string> myTestList = null;
if (myTestList != null && myTestList.Count > 0)
{
//process
}
The list is null. When processing the if statement, will it go from left to right exiting the if as soon as one condition is false?
I've tried it and seems to throw no errors, so I assume the above explains it, but I'm not sure.
Upvotes: 17
Views: 9667
Reputation: 11038
Most modern programming languages, including C#, implement what is known as McCarthy's sequential conjunction operator. This means:
a && b <=> a ? b : false
If you look at the second of these two equivalent expressions, it becomes quite clear that b
is evaluated if and only if a
is true. And conversely:
a || b <=> a ? true : b
Upvotes: 2
Reputation: 8584
For the following sample program:
void test() { bool f1 = 0; bool f2 = 0;
if (f2 && (f1 = 1)) { cout << "Inside 1 " << endl; } cout << " F1 " << f1 << " : F2 : " << f2 << endl;
if ((f1 = 1) && f2) { cout << "Inside 2 " << endl; } cout << " F1 " << f1 << " : F2 : " << f2 << endl; }
Output would be: F1 0 : F2 : 0
F1 1 : F2 : 0
Which basically shows that the if loop is being executed from left to right. 1. In the first if loop, the first condition fails and it does not bother to test the 2nd part. 2. In the second if loop, the first part is run first and then the second part.
This would work for C++. Hope this helps.
Upvotes: 0
Reputation: 1063774
It is the &&
that is important. This is short-circuiting, so the Count
is never evaluated; The conditions are evaluated left-to-right.
There is also a non-short-circuiting operator (&
), but it is very rare to see in an if test; it is mainly intended for bitwise operations (on int
etc).
From the spec:
Conditional logical operators
The
&&
and||
operators are called the conditional logical operators. They are also called the “short-circuiting” logical operators....
The
&&
and||
operators are conditional versions of the&
and|
operators:
- The operation
x && y
corresponds to the operationx & y
, except thaty
is evaluated only ifx
is notfalse
.- The operation
x || y
corresponds to the operationx | y
, except thaty
is evaluated only ifx
is nottrue
.
Upvotes: 33
Reputation: 109003
Yes, short-circuit evaluation should be used. I am not a C# developer myself, but I read this from this Wikipedia article.
Upvotes: 1
Reputation: 994221
In most (but not all) modern languages, there is a feature called "short circuit boolean evaluation". This means that if the first part of an &&
condition is false, then the second part is not evaluated at all.
A similar feature applies to ||
, where if the first part is true, the second part is not evaluated.
Note that this feature is not limited to if
statements. The following is also valid and won't try to reference Count
if myTestList
is null
:
bool b = myTestList != null && myTestList.Count > 0;
Upvotes: 9
Reputation: 523614
It's short-circuited.
a && b
if a
is false, b
will not be evaluated. We can use this behavior because the expression will always be false.
Similarly,
a || b
if a
is true, b
will not be evaluated.
Upvotes: 3
Reputation: 170519
The evaluation will stop immediately if the reference is null
. That's called logical expressions short-circuit evaluation - if the left part of &&
is false
, the right is not evaluated at all.
Upvotes: 3
Reputation: 545963
In C# and most languages, &&
and ||
are short-circuited, i.e. if the first part of the expression is enough to determine the result, the second part is guaranteed not to be executed.
The code you used is idiomatic C# code.
Upvotes: 2