By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,828 Members | 801 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,828 IT Pros & Developers. It's quick & easy.

Difference in && and &=

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
"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

P: n/a
"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

P: n/a
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

P: n/a
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.