473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 4671
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: z. f. | last post by:
Hi, i have a class that is derived from System.Web.UI.Page, and this is the class i use in my application as PageBase. all other page classes are deriverd from my PageBase instead of the...
8
by: Frank van Vugt | last post by:
Hi, If during a transaction a number of deferred triggers are fired, what will be their execution order upon the commit? Will they be executed in order of firing or alfabetically or...
23
by: roman | last post by:
Hi, I would like to have two actions for one event. But I want the second action to trigger when the first one action completes. Is it possible to do this in javascript? I'm using the onclick...
42
by: Sabiyur | last post by:
Hi all, one of the recent post gives the macro to do swap #define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp) This macro will work, if the execution is from left to right. That is step 1)...
5
by: vsteshenko | last post by:
Hello, This is my second post to the any usernet group and the first one was posted to the wrong one. I am currently working on creating an order form for sales associates at my work to be used...
1
by: zeebiggie | last post by:
Good morning I have a form with the controls in the insert statment below. table1 has an Auto increment primary key hence is omitted in the insert statment and form. Am getting the error It didnt...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.