469,926 Members | 1,492 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

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 1705
"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
By using this site, you agree to our Privacy Policy and Terms of Use.