C# If statment execution order

  • Thread starter Thread starter NvrBst
  • Start date Start date
N

NvrBst

In C# if(exp1 && exp2 && exp3)

Does it evalutate left to right (exp1 -> exp2 -> exp3)? or right to
left (exp3 -> exp2 -> exp1)? Or would it execute all of them even if
exp1 turned out to be false?

Thanks :)
 
NvrBst said:
In C# if(exp1 && exp2 && exp3)

Does it evalutate left to right (exp1 -> exp2 -> exp3)? or right to
left (exp3 -> exp2 -> exp1)? Or would it execute all of them even if
exp1 turned out to be false?

Left to right.
 
In C# if(exp1 && exp2 && exp3)

Does it evalutate left to right (exp1 -> exp2 -> exp3)? or right to
left (exp3 -> exp2 -> exp1)? Or would it execute all of them even if
exp1 turned out to be false?

It will evaluate exp1, then only if that's true will it evalute exp2,
then only if that's true will it evaluate exp3.

In other words, it does the intuitively "right" thing.

Jon
 
AND moreover it stops evaluating if an expression is false opposed to
the & operator, e.g.:

String nul = null;
String test = "test";
if (nul != null && test.Length > nul.Length)
Response.Write("test");

will NOT result in an exception, while this will:

String nul = null;
String test = "test";
if (nul != null & test.Length > nul.Length)
Response.Write("test");

because the latter will try to evaluate the expression test.Length >
nul.Length, where nul = null.

Morten
 
NvrBst said:
In C# if(exp1 && exp2 && exp3)

Does it evalutate left to right (exp1 -> exp2 -> exp3)? or right to
left (exp3 -> exp2 -> exp1)? Or would it execute all of them even if
exp1 turned out to be false?

Thanks :)

When I was forced into a VB.NET project I was laughing myself to tears when
I learned about the AndAlso and OrElse keywords.

But to be fair, guess they had add those keywords do make it easier to port
VB6 code without breaking logic.

- Michael S
 
Maate said:
String nul = null;
String test = "test";
if (nul != null & test.Length > nul.Length)
Response.Write("test");

I really hope you don't call variables nul in production code.

Michael
 
Michael S said:
When I was forced into a VB.NET project I was laughing myself to tears
when I learned about the AndAlso and OrElse keywords.

But to be fair, guess they had add those keywords do make it easier to
port VB6 code without breaking logic.

They really can't win, they change things people complain, if they leave
them the same (and add new keyword for the changes) then people complain.
 
Michael said:
I really hope you don't call variables nul in production code.

There are great potential for obfuscating code.

if(nul == null || Null == null || nulL == null || Null == nul || nulL ==
Null)

:-)

Arne
 
Michael said:
When I was forced into a VB.NET project I was laughing myself to tears when
I learned about the AndAlso and OrElse keywords.

The short circuit is very practical.

The situation where the second test can not be executed if
the first test does not succeed is very common.

if(x != null && x.something())

But from a puristic point of view, then the alternative
tend to enforce more clear code.

if(a && b.changesomeglobalstate())

Arne
 
Arne Vajhøj said:
The short circuit is very practical.

The situation where the second test can not be executed if
the first test does not succeed is very common.

if(x != null && x.something())

But from a puristic point of view, then the alternative
tend to enforce more clear code.

if(a && b.changesomeglobalstate())

I'd rather have practical than pure any day :)

However, you can always use the non-short-circuiting "&" operator
instead:

if (a & b.ChangeSomeGlobalState())
....
 
Arne Vajhøj said:
There are great potential for obfuscating code.

if(nul == null || Null == null || nulL == null || Null == nul || nulL ==
Null)

After writing such code you will be truly irreplacable.

Michael
 
Michael said:
After writing such code you will be truly irreplacable.

Michael

const True = false;
const fa1se = !True;

also helps. ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top