473,407 Members | 2,359 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,407 software developers and data experts.

Logical AND (&&) in an If Statement

This is a very basic question, but I can' turn up a statement on the
subject:

In C#, if I have

If (x == 1 && y == 2)
{
....
}

Suppose x is not equal to 1, does the logical test for y == 2 still get
executed?

TIA

Charles
Jun 27 '08 #1
7 1710
Hello Charles,

With && if the frist part is false the second part is not evaluated AKA
"short circuit evaluation".
With a single & the second part is evaluated unconditionally.

http://msdn.microsoft.com/en-us/libr...dk(VS.71).aspx

Regards,
Leo
"Charles Law" <bl***@nowhere.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
This is a very basic question, but I can' turn up a statement on the
subject:

In C#, if I have

If (x == 1 && y == 2)
{
...
}

Suppose x is not equal to 1, does the logical test for y == 2 still get
executed?

TIA

Charles

Jun 27 '08 #2
No; the && and || operators are short-circuited:

* if the first operand in a && is false, then false is returned
without looking at the second operand
* if the first operand in a || is true, then true is returned without
looking at the second operand

Marc
Jun 27 '08 #3
On May 7, 7:55*am, "Charles Law" <bl...@nowhere.comwrote:
This is a very basic question, but I can' turn up a statement on the
subject:

In C#, if I have

If (x == 1 && y == 2)
{
...

}

Suppose x is not equal to 1, does the logical test for y == 2 still get
executed?

TIA

Charles
If you have any VB.NET experiance, the && and || operators are
equivalent to the VB.NET AndAlso and OrElse operators.
Jun 27 '08 #4
Thanks Leo and Marc for the replies.

What was concerning me was that in the following:

const int expectedlength = 10;

byte[] myarray;

// assign to myarray
...

If (myarray.GetLength(0) == expectedlength && myarray[expectedlength -
1] == 0x1)
{
...
}

If the array was not the expected length then the subsequent test could
cause an IndexOutOfRange exception.

Charles
"Marc Gravell" <ma**********@gmail.comwrote in message
news:13**********************************@m36g2000 hse.googlegroups.com...
No; the && and || operators are short-circuited:

* if the first operand in a && is false, then false is returned
without looking at the second operand
* if the first operand in a || is true, then true is returned without
looking at the second operand

Marc

Jun 27 '08 #5
On May 7, 2:26 pm, "Charles Law" <bl...@nowhere.comwrote:
Thanks Leo and Marc for the replies.

What was concerning me was that in the following:

const int expectedlength = 10;

byte[] myarray;

// assign to myarray
...

If (myarray.GetLength(0) == expectedlength && myarray[expectedlength -
1] == 0x1)
{
...
}

If the array was not the expected length then the subsequent test could
cause an IndexOutOfRange exception.
No, that should be fine. I'd use the Length property instead of
GetLength(0) though, just for the sake of readability.

Jon
Jun 27 '08 #6
Hi Jon

Thanks for the confirmation; and noted about the Length suggestion.

Cheers

Charles
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:27**********************************@l64g2000 hse.googlegroups.com...
On May 7, 2:26 pm, "Charles Law" <bl...@nowhere.comwrote:
>Thanks Leo and Marc for the replies.

What was concerning me was that in the following:

const int expectedlength = 10;

byte[] myarray;

// assign to myarray
...

If (myarray.GetLength(0) == expectedlength &&
myarray[expectedlength -
1] == 0x1)
{
...
}

If the array was not the expected length then the subsequent test could
cause an IndexOutOfRange exception.

No, that should be fine. I'd use the Length property instead of
GetLength(0) though, just for the sake of readability.

Jon


Jun 27 '08 #7

Hi Zacks
If you have any VB.NET experiance, the && and || operators are
equivalent to the VB.NET AndAlso and OrElse operators.
I have, as I generally use VB.NET, so that makes sense to me.

Thanks.

Charles
<za***@construction-imaging.comwrote in message
news:2a**********************************@a23g2000 hsc.googlegroups.com...
On May 7, 7:55 am, "Charles Law" <bl...@nowhere.comwrote:
This is a very basic question, but I can' turn up a statement on the
subject:

In C#, if I have

If (x == 1 && y == 2)
{
...

}

Suppose x is not equal to 1, does the logical test for y == 2 still get
executed?

TIA

Charles
If you have any VB.NET experiance, the && and || operators are
equivalent to the VB.NET AndAlso and OrElse operators.
Jun 27 '08 #8

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

Similar topics

7
by: GrandpaB | last post by:
While writing this plea for help, I think I solved my dilemma, but I don't know why the statement that solved the problem is necessary. The inspiration for the statement came from an undocumented...
3
by: serge | last post by:
How do I determine which method I should use if I want to optimize the performance of a database. I took Northwind's database to run my example. My query is I want to retrieve the Employees'...
4
by: Ashish_CPP | last post by:
Hi, Can anyone help me and suggest some good C books that enhances the logical & problem solving skills. I dont want those books which teach C/C++ rather I want those books to be suggested that...
9
by: marko | last post by:
/* code start */ int a = 0; /* expected evaluation and excution order with precedence in mind /* False(3) , True(1), False(2) */ if ( (a=1) == 0 || 0 != 1 && (a =2) == 1) putchar('T');...
0
by: Roald | last post by:
I am working on an application that needs to implement logical deletes instead of removing the records permanently. The logical delete works by setting 3 fields (deleted flag, date and user). I...
3
by: nkumarin001 | last post by:
Hi, a) Why oracle architecture is divided into 2 types logical and physical structure? b) What is the use of logical structure in oracle as it does not consume any space if so then...
2
by: Old Fossil Bama | last post by:
In DB2/400, if I run this statement: delete from ordvcrec a where not exists(select * from i$paint b where a.part#=b.i$part) Where ORDVCREC is a join logical of physical files ORDVALID and...
11
by: Dominic Vella | last post by:
I am using MS-Access2000. I can't seem to set the default values for Logical type fields. I start with Dim dbsTmp As Object ' I think it's DAO.Database Set dbsTmp =...
0
by: prassaad | last post by:
Hi everyone! I don't understand what is actual difference between logical & raw reading happens during copying data.... I m waiting......
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.