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

Re: Logical AND (&&) in an If Statement by Leo

Leo
Wed May 07 07:07:53 CDT 2008

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/library/2a723cdk(VS.71).aspx

Regards,
Leo


"Charles Law" <blank@nowhere.com> wrote in message
news:%23R3JFlDsIHA.3804@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
>
>


Re: Logical AND (&&) in an If Statement by Marc

Marc
Wed May 07 07:22:58 CDT 2008

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

Re: Logical AND (&&) in an If Statement by zacks

zacks
Wed May 07 08:27:08 CDT 2008

On May 7, 7:55=A0am, "Charles Law" <bl...@nowhere.com> wrote:
> This is a very basic question, but I can' turn up a statement on the
> subject:
>
> In C#, if I have
>
> If (x =3D=3D 1 && y =3D=3D 2)
> {
> ...
>
> }
>
> Suppose x is not equal to 1, does the logical test for y =3D=3D 2 still ge=
t
> executed?
>
> TIA
>
> Charles

If you have any VB.NET experiance, the && and || operators are
equivalent to the VB.NET AndAlso and OrElse operators.

Re: Logical AND (&&) in an If Statement by Charles

Charles
Wed May 07 08:26:30 CDT 2008

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" <marc.gravell@gmail.com> wrote in message
news:133a74d5-67b8-4c89-b8c5-a8c501152062@m36g2000hse.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



Re: Logical AND (&&) in an If Statement by Jon

Jon
Wed May 07 08:32:12 CDT 2008

On May 7, 2:26 pm, "Charles Law" <bl...@nowhere.com> wrote:
> 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



Re: Logical AND (&&) in an If Statement by Charles

Charles
Wed May 07 09:37:29 CDT 2008

Hi Jon

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

Cheers

Charles


"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:27300c40-2300-4865-9bfe-27f251b17043@l64g2000hse.googlegroups.com...
> On May 7, 2:26 pm, "Charles Law" <bl...@nowhere.com> wrote:
>> 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
>
>



Re: Logical AND (&&) in an If Statement by Charles

Charles
Wed May 07 09:43:59 CDT 2008


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


<zacks@construction-imaging.com> wrote in message
news:2acc3b1a-9203-4700-b84a-1f52b402bfac@a23g2000hsc.googlegroups.com...
On May 7, 7:55 am, "Charles Law" <bl...@nowhere.com> wrote:
> 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.