469,358 Members | 1,473 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

C# If statment execution order

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 :)

Jun 13 '07 #1
11 4505
"NvrBst" <nv****@gmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
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.
>
Thanks :)

Jun 13 '07 #2
On Jun 13, 9:14 am, NvrBst <nvr...@gmail.comwrote:
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

Jun 13 '07 #3

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
Jun 13 '07 #4
"NvrBst" <nv****@gmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
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
Jun 14 '07 #5
"Maate" <mo*********@gmail.comwrote in message
news:11*********************@g37g2000prf.googlegro ups.com...
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
Jun 14 '07 #6
"Michael S" <no@no.nowrote in message
news:uG*************@TK2MSFTNGP06.phx.gbl...
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 S

Jun 14 '07 #7
Michael C wrote:
"Maate" <mo*********@gmail.comwrote in message
news:11*********************@g37g2000prf.googlegro ups.com...
>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.
There are great potential for obfuscating code.

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

:-)

Arne
Jun 17 '07 #8
Michael S wrote:
"NvrBst" <nv****@gmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
>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?
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

Jun 17 '07 #9
Arne Vajh°j <ar**@vajhoej.dkwrote:
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())
....

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 17 '07 #10
"Arne Vajh°j" <ar**@vajhoej.dkwrote in message
news:46***********************@news.sunsite.dk...
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
Jun 18 '07 #11
Michael C wrote:
"Arne Vajh°j" <ar**@vajhoej.dkwrote in message
news:46***********************@news.sunsite.dk...
>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
const True = false;
const fa1se = !True;

also helps. ;)

--
G÷ran Andersson
_____
http://www.guffa.com
Jun 18 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by roman | last post: by
42 posts views Thread by Sabiyur | last post: by
1 post views Thread by zeebiggie | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.