473,490 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Multiple Conditions Firing order

Hi,

Does anyone know if these 2 code segments would operate the same way.

for instance if the first condition fails does it bother to check the
second?

if (MyList.Count 0 && CheckDate) {

// do something...

}

or

if(MyList.Count)
{
if(CheckDate)
{
// do something...
}
}

public bool CheckDate()
{
return true;
}

Mar 27 '07 #1
13 2062
In C#, like C, logic is short-circuiting. So when you have if(a && b)
it will only check b if a is true, and when you have if(a || b) it
will only check b if a is NOT true; otherwise it will return after
checking a. That's why you can get away with if(object != null &&
object.Property == something). In VB, the And and Or logic is NOT
short-circuting, you have to use AndAlso and OrElse for this.

With that said, I would assume the two are equivalent but there could
be a subtle difference, I will defer to somebody else on that.
On Mar 26, 6:31 pm, "TheLostLeaf" <e...@canyondigital.comwrote:
Hi,

Does anyone know if these 2 code segments would operate the same way.

for instance if the first condition fails does it bother to check the
second?

if (MyList.Count 0 && CheckDate) {

// do something...

}

or

if(MyList.Count)
{
if(CheckDate)
{
// do something...
}

}

public bool CheckDate()
{
return true;

}- Hide quoted text -

- Show quoted text -

Mar 27 '07 #2
Thanks !

I wanted to double check, but that makes sense. Less code is always
nicer to get the same result.

Mar 27 '07 #3
TheLostLeaf <er**@canyondigital.comwrote:
Thanks !

I wanted to double check, but that makes sense. Less code is always
nicer to get the same result.
Note that only && and || are short-circuiting. If you change your
condition to

if (MyList.Count 0 & CheckDate)

it will always test both conditions.

--
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
Mar 27 '07 #4
On Mon, 26 Mar 2007 23:30:29 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
Note that only && and || are short-circuiting. If you change your
condition to

if (MyList.Count 0 & CheckDate)

it will always test both conditions.
Can you clarify? I only see one condition in that clause.

Pete
Mar 27 '07 #5
On Mar 27, 4:13 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
Note that only && and || are short-circuiting. If you change your
condition to
if (MyList.Count 0 & CheckDate)
it will always test both conditions.

Can you clarify? I only see one condition in that clause.
MyList.Count and CheckDate will both be evaluated in the above,
whereas the version with && won't evaluate CheckDate if MyList.Count
isn't positive.

Jon

Mar 27 '07 #6
On Mar 27, 7:13 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Mon, 26 Mar 2007 23:30:29 -0700, Jon Skeet [C# MVP] <s...@pobox.com>
wrote:
Note that only && and || are short-circuiting. If you change your
condition to
if (MyList.Count 0 & CheckDate)
it will always test both conditions.

Can you clarify? I only see one condition in that clause.
The binary '&' operator serves two masters. From MSDN:

Binary & operators are predefined for the integral types and bool. For
integral types, & computes the logical bitwise AND of its operands.
For bool operands, & computes the logical AND of its operands; that
is, the result is true if and only if both its operands are true.

Since '0' and 'CheckDate' don't have matching types, this ambiguity
isn't an issue in the expression above.

Michael
Mar 27 '07 #7
On Tue, 27 Mar 2007 08:37:13 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
if (MyList.Count 0 & CheckDate)
it will always test both conditions.

Can you clarify? I only see one condition in that clause.

MyList.Count and CheckDate will both be evaluated in the above,
whereas the version with && won't evaluate CheckDate if MyList.Count
isn't positive.
But there's still only one condition. That is, there is no conditional
operator, so the entire clause is a single condition. In that situation,
it should be no surprise that everything gets evaluated, because there's
nothing to short-circuit.

Or was that your point?

FWIW, I found the statement "it will always test both conditions"
confusing, because of the fact that there is just the single condition in
the example.

Pete
Mar 27 '07 #8
On Tue, 27 Mar 2007 09:07:54 -0700, mp*******@gmail.com
<mp*******@gmail.comwrote:
The binary '&' operator serves two masters. From MSDN:

Binary & operators are predefined for the integral types and bool. For
integral types, & computes the logical bitwise AND of its operands.
For bool operands, & computes the logical AND of its operands; that
is, the result is true if and only if both its operands are true.

Since '0' and 'CheckDate' don't have matching types, this ambiguity
isn't an issue in the expression above.
The relational operator has higher precedence than the logical &
operator, so both operands of the & operator are boolean, and so the
so-called ambiguity would in fact be an issue, if it existed.
"MyList.Count 0" is evaluated before the rest of the clause.

However, I don't see it as an ambiguity anyway. The logical & operator
does the same thing with two booleans that it would do with two ints. It
just doesn't bother to promote the result to an int. The behavior doesn't
really change depending on the type, any more than the * operator's
behavior changes according to operand type (use * with two ints, you get
an int, use it with two longs, you get a long).

But the & operator is not a conditional operator, and since one should
expect only a conditional operator to short-circuit, I don't find the lack
of short-circuiting surprising at all.

C/C++ works in exactly the same way. It has the same logical & operator,
and that operator always requires both of its operands to be evaluated.

Pete
Mar 27 '07 #9
On Mar 27, 8:24 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Tue, 27 Mar 2007 09:07:54 -0700, mpetro...@gmail.com

<mpetro...@gmail.comwrote:
The binary '&' operator serves two masters. From MSDN:
Binary & operators are predefined for the integral types and bool. For
integral types, & computes the logical bitwise AND of its operands.
For bool operands, & computes the logical AND of its operands; that
is, the result is true if and only if both its operands are true.
Since '0' and 'CheckDate' don't have matching types, this ambiguity
isn't an issue in the expression above.

The relational operator has higher precedence than the logical &
operator, so both operands of the & operator are boolean, and so the
so-called ambiguity would in fact be an issue, if it existed.
"MyList.Count 0" is evaluated before the rest of the clause.

However, I don't see it as an ambiguity anyway. The logical & operator
does the same thing with two booleans that it would do with two ints. It
just doesn't bother to promote the result to an int. The behavior doesn't
really change depending on the type, any more than the * operator's
behavior changes according to operand type (use * with two ints, you get
an int, use it with two longs, you get a long).

But the & operator is not a conditional operator, and since one should
expect only a conditional operator to short-circuit, I don't find the lack
of short-circuiting surprising at all.

C/C++ works in exactly the same way. It has the same logical & operator,
and that operator always requires both of its operands to be evaluated.
Peter, I was talking about ambiguity reading the code. I'm aware that
C#'s operator precedence rules are well defined. It seemed to me,
though, that the OP didn't understand the relative precedence of '>'
and '&' (and I confess that I'd need to look up the order of '|' and
'^', for instance).

Parentheses are cheap. There's no reason to go with
if (x || y && z)
when you could use
if (x || (y && z))

Michael
Mar 27 '07 #10
On Tue, 27 Mar 2007 10:47:35 -0700, mp*******@gmail.com
<mp*******@gmail.comwrote:
[...]
Parentheses are cheap. There's no reason to go with
if (x || y && z)
when you could use
if (x || (y && z))
I agree...I very often use parentheses when not needed, simply because it
enhances the readability of the code.

Operator precedence is important for the sake of having a language
well-defined, but I prefer to make things explicit except in the simplest
cases.

Pete
Mar 27 '07 #11
Peter Duniho <Np*********@nnowslpianmk.comwrote:
MyList.Count and CheckDate will both be evaluated in the above,
whereas the version with && won't evaluate CheckDate if MyList.Count
isn't positive.

But there's still only one condition. That is, there is no conditional
operator, so the entire clause is a single condition. In that situation,
it should be no surprise that everything gets evaluated, because there's
nothing to short-circuit.

Or was that your point?

FWIW, I found the statement "it will always test both conditions"
confusing, because of the fact that there is just the single condition in
the example.
I was trying to use the same terminology as the OP. If we're going to
use spec terminology, && is a "conditional logical operator" and I
don't think the spec defines "condition" at all.

My sole point was that && behaves differently to &. Yes, many people
would expect that without being told - but it wasn't clear whether the
OP would know it, so I pointed it out. As the OP was calling the two
boolean sub-expressions "conditions" I decided to follow suit.

--
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
Mar 27 '07 #12
Peter Duniho <Np*********@nnowslpianmk.comwrote:
[...]
Parentheses are cheap. There's no reason to go with
if (x || y && z)
when you could use
if (x || (y && z))

I agree...I very often use parentheses when not needed, simply because it
enhances the readability of the code.

Operator precedence is important for the sake of having a language
well-defined, but I prefer to make things explicit except in the simplest
cases.
Absolutely. I've very deliberately not learned the precedence of
operators (beyond "a*b + c" etc) so that I won't be able to assume that
anyone else has either :)

--
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
Mar 27 '07 #13
On Tue, 27 Mar 2007 11:10:24 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
I was trying to use the same terminology as the OP. [...]

As the OP was calling the two
boolean sub-expressions "conditions" I decided to follow suit.
I see...fair enough.
Mar 27 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
31157
by: john | last post by:
How do u guys handle multiple sessions?? i.e, opening different browser windows by running iexplore.exe or clicking IE icons and opening the application. My sessions are mixing up. what i mean is...
3
7736
by: Gord | last post by:
I would like to create a summary report from the results of 11 queries (based on 2 tables). All the queries have the same format and return 3 numbers (Count, Current Year Bal, Last Year Bal.)...
2
6509
by: Jen F. | last post by:
I have inherited a medical database in which there are multiple values stored in a single field (ie. "Current Conditions" field might contain 1-20 different conditions, separated by comma (ie....
1
4462
by: Jo | last post by:
I am having a real problem with the Launch conditions in VS .NET and can only come to the conclusion that it is a bug. It states quite emphatically in the MSDN that Launch Conditions WILL be...
7
3367
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
52
6268
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
2
2686
by: BK | last post by:
Can anyone point me to documentation on the firing order for events in a form? Years ago, I programmed in FoxPro and the order was Load, Init, Show, Activate, GotFocus (LISA G was the acronymn I...
2
2568
by: Igor | last post by:
1. Are stored procedures WITH ENCRYPTION slower than the ones without encryption? 2. Should i put most restrictive conditions first or last in WHERE? In which order does MSSQL execute...
3
2883
by: dinox | last post by:
hello, i tried searching for something similar to my situation but i haven't found so far.. for example ... i have users table with 15 registered users and i want to show only 5 of them in front...
0
7112
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
7183
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6852
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.