views:

280

answers:

8

Hey guys,

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, 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.

Thanks in advance.

+16  A: 

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 operation x & y, except that y is evaluated only if x is not false.
  • The operation x || y corresponds to the operation x | y, except that y is evaluated only if x is not true.
Marc Gravell
+1  A: 

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.

Konrad Rudolph
A: 

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.

sharptooth
+1  A: 

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.

KennyTM
+5  A: 

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;
Greg Hewgill
+1  A: 

Yes, short-circuit evaluation should be used. I am not a C# developer myself, but I read this from this Wikipedia article.

Andreas Rejbrand
+2  A: 

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
Gorpik
A: 

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.

  • Ivar
topgun_ivard