Seibar
Seibar

Reputation: 70253

C# logic order and compiler behavior

In C#, (and feel free to answer for other languages), what order does the runtime evaluate a logic statement?

Example:

DataTable myDt = new DataTable();
if (myDt != null && myDt.Rows.Count > 0)
{
    //do some stuff with myDt
}

Which statement does the runtime evaluate first -

myDt != null

or:

myDt.Rows.Count > 0

?

Is there a time when the compiler would ever evaluate the statement backwards? Perhaps when an "OR" operator is involved?


& is known as a logical bitwise operator and will always evaluate all the sub-expressions

What is a good example of when to use the bitwise operator instead of the "short-circuited boolean"?

Upvotes: 18

Views: 4124

Answers (18)

Brian Leahy
Brian Leahy

Reputation: 35517

"C# : Left to right, and processing stops if a match (evaluates to true) is found."

Zombie sheep is wrong.

The question is about the && operator, not the || operator.

In the case of && evaluation will stop if a FALSE is found.

In the case of || evaluation stops if a TRUE is found.

Upvotes: 9

BCS
BCS

Reputation: 78595

The D programming language Does do left-to-right evaluation with short circuiting and doesn't allow overloading of the && and '||' operators.

Upvotes: 0

ZombieSheep
ZombieSheep

Reputation: 29953

C# : Left to right, and processing stops if a non-match (evaluates to false) is found.

Upvotes: 16

Konrad Rudolph
Konrad Rudolph

Reputation: 545618

@csmba:

It was asked in a followup why or when would anyone use And instead of AndAlso (or & instead of &&): Here is an example:

if ( x.init() And y.init()) then
   x.process(y)
end 
y.doDance()

In this case, I want to init both X and Y. Y must be initialized in order for y.DoDance to be able to execute. However, in the init() function I am doing also some extra thing like checking a socket is open, and only if that works out ok, for both, I should go ahead and do the x.process(y).

I believe this is rather confusing. Although your example works, it's not the typical case for using And (and I would probably write this differently to make it clearer). And (& in most other languages) is actually the bitwise-and operation. You would use it to calculate bit operations, for example deleting a flag bit or masking and testing flags:

Dim x As Formatting = Formatting.Bold Or Formatting.Italic
If (x And Formatting.Italic) = Formatting.Italic Then
    MsgBox("The text will be set in italic.")
End If

Upvotes: 0

Bruce Alderman
Bruce Alderman

Reputation: 2274

What is a good example of when to use the bitwise operator instead of the "short-circuited boolean"?

Suppose you have flags, say for file attributes. Suppose you've defined READ as 4, WRITE as 2, and EXEC as 1. In binary, that's:

READ  0100  
WRITE 0010  
EXEC  0001

Each flag has one bit set, and each one is unique. The bitwise operators let you combine these flags:

flags = READ & EXEC; // value of flags is 0101

Upvotes: 1

csmba
csmba

Reputation: 4083

vb.net

if( x isNot Nothing AndAlso x.go()) then
  1. Evaluation is done left to right
  2. AndAlso operator makes sure that only if the left side was TRUE, the right side will be evaluated (very important, since ifx is nothing x.go will crash)

You may use And instead ofAndAlso in vb. in which case the left side gets evaluated first as well, but the right side will get evaluated regardless of result.

Best Practice: Always use AndAlso, unless you have a very good reason why not to.


It was asked in a followup why or when would anyone use And instead of AndAlso (or & instead of &&): Here is an example:

if ( x.init() And y.init()) then
   x.process(y)
end 
y.doDance()

In this case, I want to init both X and Y. Y must be initialized in order for y.DoDance to be able to execute. However, in the init() function I am doing also some extra thing like checking a socket is open, and only if that works out ok, for both, I should go ahead and do the x.process(y).

Again, this is probably not needed and not elegant in 99% of the cases, that is why I said that the default should be to use AndAlso.

Upvotes: 6

C. K. Young
C. K. Young

Reputation: 223073

I like Orion's responses. I'll add two things:

  1. The left-to-right still applies first
  2. The inner-to-outer to ensure that all arguments are resolved before calling the function

Say we have the following example:

a = Foo(5, GetSummary("Orion", GetAddress("Orion")),
           GetSummary("Chris", GetAddress("Chris")));

Here's the order of execution:

  1. GetAddress("Orion")
  2. GetSummary("Orion", ...)
  3. GetAddress("Chris")
  4. GetSummary("Chris", ...)
  5. Foo(...)
  6. Assigns to a

I can't speak about C#'s legal requirements (although I did test a similar example using Mono before writing this post), but this order is guaranteed in Java.

And just for completeness (since this is a language-agnostic thread as well), there are languages like C and C++, where the order is not guaranteed unless there is a sequence point. References: 1, 2. In answering the thread's question, however, && and || are sequence points in C++ (unless overloaded; also see OJ's excellent answer). So some examples:

  • foo() && bar()
  • foo() & bar()

In the && case, foo() is guaranteed to run before bar() (if the latter is run at all), since && is a sequence point. In the & case, no such guarantee is made (in C and C++), and indeed bar() can run before foo(), or vice versa.

Upvotes: 1

OJ.
OJ.

Reputation: 29401

I realise this question has already been answered, but I'd like to throw in another bit of information which is related to the topic.

In languages, like C++, where you can actually overload the behaviour of the && and || operators, it is highly recommended that you do not do this. This is because when you overload this behaviour, you end up forcing the evaluation of both sides of the operation. This does two things:

  1. It breaks the lazy evaluation mechanism because the overload is a function which has to be invoked, and hence both parameters are evaluated before calling the function.
  2. The order of evaluation of said parameters isn't guaranteed and can be compiler specific. Hence the objects wouldn't behave in the same manner as they do in the examples listed in the question/previous answers.

For more info, have a read of Scott Meyers' book, More Effective C++. Cheers!

Upvotes: 8

Orion Edwards
Orion Edwards

Reputation: 123652

When things are all in-line, they're executed left-to-right.

When things are nested, they're executed inner-to-outer. This may seem confusing as usually what's "innermost" is on the right-hand side of the line, so it seems like it's going backwards...

For example

a = Foo( 5, GetSummary( "Orion", GetAddress("Orion") ) );

Things happen like this:

  • Call GetAddress with the literal "Orion"
  • Call GetSummary with the literal "Orion" and the result of GetAddress
  • Call Foo with the literal 5 and the result of GetSummary
  • Assign this value to a

Upvotes: 1

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391396

You use & when you specifically want to evaluate all the sub-expressions, most likely because they have side-effects you want, even though the final result will be false and thus not execute your then part of your if-statement.

Note that & and | operates for both bitwise masks and boolean values and is not just for bitwise operations. They're called bitwise, but they are defined for both integers and boolean data types in C#.

Upvotes: 0

Orion Edwards
Orion Edwards

Reputation: 123652

@shsteimer

The concept modesty is referring to is operator overloading. in the statement: ... A is evaluated first, if it evaluates to false, B is never evaluated. The same applies to

That's not operator overloading. Operator overloading is the term given for letting you define custom behaviour for operators, such as *, +, = and so on.

This would let you write your own 'Log' class, and then do

a = new Log(); // Log class overloads the + operator
a + "some string"; // Call the overloaded method - otherwise this wouldn't work because you can't normally add strings to objects.

Doing this

a() || b() // be never runs if a is true

is actually called Short Circuit Evaluation

Upvotes: 5

Mike Stone
Mike Stone

Reputation: 44613

Some languages have interesting situations where expressions are executed in a different order. I am specifically thinking of Ruby, but I'm sure they borrowed it from elsewhere (probably Perl).

The expressions in the logic will stay left to right, but for example:

puts message unless message.nil?

The above will evaluate "message.nil?" first, then if it evaluates to false (unless is like if except it executes when the condition is false instead of true), "puts message" will execute, which prints the contents of the message variable to the screen.

It's kind of an interesting way to structure your code sometimes... I personally like to use it for very short 1 liners like the above.

Edit:

To make it a little clearer, the above is the same as:

unless message.nil?
  puts message
end

Upvotes: 4

Vaibhav
Vaibhav

Reputation: 11436

Nopes, at least the C# compiler doesn't work backwards (in either && or ||). It's left to right.

Upvotes: 1

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391396

Note that there is a difference between && and & regarding how much of your expression is evaluated.

&& is known as a short-circuited boolean AND, and will, as noted by others here, stop early if the result can be determined before all the sub-expressions are evaluated.

& is known as a logical bitwise operator and will always evaluate all the sub-expressions.

As such:

if (a() && b())

Will only call b if a returns true.

however, this:

if (a() & b())

Will always call both a and b, even though the result of calling a is false and thus known to be false regardless of the result of calling b.

This same difference exists for the || and | operators.

Upvotes: 4

Brad Tutterow
Brad Tutterow

Reputation: 7487

ZombieSheep is dead-on. The only "gotcha" that might be waiting is that this is only true if you are using the && operator. When using the & operator, both expressions will be evaluated every time, regardless if one or both evaluate to false.

if (amHungry & whiteCastleIsNearby)
{
   // The code will check if White Castle is nearby
   // even when I am not hungry
}

if (amHungry && whiteCastleIsNearby)
{
   // The code will only check if White Castle is nearby
   // when I am hungry
}

Upvotes: 4

shsteimer
shsteimer

Reputation: 28810

The concept modesty is referring to is operator overloading. in the statement:

if( A && B){
    // do something
}

A is evaluated first, if it evaluates to false, B is never evaluated. The same applies to

if(A || B){
    //do something
}

A is evaluated first, if it evaluates to true, B is never evaluated.

This concept, overloading, applies to (i think) all of the C style languages, and many others as well.

Upvotes: 2

GateKiller
GateKiller

Reputation: 75869

I have heard somewhere that compilers work backwards, but I am unsure how true this is.

Upvotes: 0

Shawn
Shawn

Reputation: 19803

The left one, then stops if it is null.

Edit: In vb.net it will evaluate both and possibly throw an error, unless you use AndAlso

Upvotes: 2

Related Questions