472,102 Members | 1,087 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Difference in && and &=

Run this method:

public void test()
{
bool b;
int i=0;

b=false;
i=0;
b=(b && i++==1);
Console.WriteLine(i.ToString());

b=false;
i=0;
b&=i++==1;
Console.WriteLine(i.ToString());
}

Notice how in the first syntax, i++ is NOT evaluated (because of
short-circuit evaluation) but in the second syntax i++ is evaluated. I
wonder if this was intentional or an oversight. It burned me in a situation
when I was counting on short-circuit evaluation.

<joel>
Nov 16 '05 #1
4 1782
"Joel" <jo******@hotmail.com> wrote in
news:eI**************@TK2MSFTNGP15.phx.gbl:
Run this method:

public void test()
{
bool b;
int i=0;

b=false;
i=0;
b=(b && i++==1);
Console.WriteLine(i.ToString());

b=false;
i=0;
b&=i++==1;
Console.WriteLine(i.ToString());
}

Notice how in the first syntax, i++ is NOT evaluated (because of
short-circuit evaluation) but in the second syntax i++ is
evaluated. I wonder if this was intentional or an oversight. It
burned me in a situation when I was counting on short-circuit
evaluation.


Joel,

& and && are two completely different operators. & performs a
bitwise "and" of two numbers, but && is the boolean "and" operator.
Change the && to & and both code blocks return the same result (1).

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #2
"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message news:<Xn**********************************@207.46. 248.16>...
"Joel" <jo******@hotmail.com> wrote in
news:eI**************@TK2MSFTNGP15.phx.gbl:
Run this method:

public void test()
{
bool b;
int i=0;

b=false;
i=0;
b=(b && i++==1);
Console.WriteLine(i.ToString());

b=false;
i=0;
b&=i++==1;
Console.WriteLine(i.ToString());
}

Notice how in the first syntax, i++ is NOT evaluated (because of
short-circuit evaluation) but in the second syntax i++ is
evaluated. I wonder if this was intentional or an oversight. It
burned me in a situation when I was counting on short-circuit
evaluation.


Joel,

& and && are two completely different operators. & performs a
bitwise "and" of two numbers, but && is the boolean "and" operator.
Change the && to & and both code blocks return the same result (1).

Is short-circuit evaluation the same as lazy evaluation?
Nov 16 '05 #3
be*********@yahoo.com (Xarky) wrote in
news:bc*************************@posting.google.co m:
"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote
in message
news:<Xn**********************************@207.46. 248.16>...
"Joel" <jo******@hotmail.com> wrote in
news:eI**************@TK2MSFTNGP15.phx.gbl:
> Run this method:
>
> public void test()
> {
> bool b;
> int i=0;
>
> b=false;
> i=0;
> b=(b && i++==1);
> Console.WriteLine(i.ToString());
>
> b=false;
> i=0;
> b&=i++==1;
> Console.WriteLine(i.ToString());
> }
>
> Notice how in the first syntax, i++ is NOT evaluated (because
> of short-circuit evaluation) but in the second syntax i++ is
> evaluated. I wonder if this was intentional or an oversight.
> It burned me in a situation when I was counting on
> short-circuit evaluation.


Joel,

& and && are two completely different operators. & performs a
bitwise "and" of two numbers, but && is the boolean "and"
operator. Change the && to & and both code blocks return the
same result (1).

Is short-circuit evaluation the same as lazy evaluation?

http://en.wikipedia.org/wiki/Lazy_evaluation

and

http://en.wikipedia.org/wiki/Short_circuit

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #4
Yeah, I'm aware of the difference between & and && (and and and? ;-). Just
pointing out a 'gotcha' that 'gotme'.

</joel>

"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
news:Xn**********************************@207.46.2 48.16...
be*********@yahoo.com (Xarky) wrote in
news:bc*************************@posting.google.co m:
"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote
in message
news:<Xn**********************************@207.46. 248.16>...
"Joel" <jo******@hotmail.com> wrote in
news:eI**************@TK2MSFTNGP15.phx.gbl:

> Run this method:
>
> public void test()
> {
> bool b;
> int i=0;
>
> b=false;
> i=0;
> b=(b && i++==1);
> Console.WriteLine(i.ToString());
>
> b=false;
> i=0;
> b&=i++==1;
> Console.WriteLine(i.ToString());
> }
>
> Notice how in the first syntax, i++ is NOT evaluated (because
> of short-circuit evaluation) but in the second syntax i++ is
> evaluated. I wonder if this was intentional or an oversight.
> It burned me in a situation when I was counting on
> short-circuit evaluation.

Joel,

& and && are two completely different operators. & performs a
bitwise "and" of two numbers, but && is the boolean "and"
operator. Change the && to & and both code blocks return the
same result (1).

Is short-circuit evaluation the same as lazy evaluation?

http://en.wikipedia.org/wiki/Lazy_evaluation

and

http://en.wikipedia.org/wiki/Short_circuit

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Mattias Campe | last post: by
3 posts views Thread by Stefan Weiss | last post: by
2 posts views Thread by lf | last post: by
16 posts views Thread by Steven T. Hatton | last post: by
27 posts views Thread by Daniel Vallstrom | last post: by
2 posts views Thread by Eric | last post: by
3 posts views Thread by Tony | last post: by
reply views Thread by leo001 | last post: by

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.