473,605 Members | 2,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference in && and &=

Run this method:

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

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

b=false;
i=0;
b&=i++==1;
Console.WriteLi ne(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 1890
"Joel" <jo******@hotma il.com> wrote in
news:eI******** ******@TK2MSFTN GP15.phx.gbl:
Run this method:

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

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

b=false;
i=0;
b&=i++==1;
Console.WriteLi ne(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_NO SPAM_Xcrtimmons inc.com> wrote in message news:<Xn******* *************** ************@20 7.46.248.16>...
"Joel" <jo******@hotma il.com> wrote in
news:eI******** ******@TK2MSFTN GP15.phx.gbl:
Run this method:

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

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

b=false;
i=0;
b&=i++==1;
Console.WriteLi ne(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*********@yah oo.com (Xarky) wrote in
news:bc******** *************** **@posting.goog le.com:
"Chris R. Timmons" <crtimmons@X_NO SPAM_Xcrtimmons inc.com> wrote
in message
news:<Xn******* *************** ************@20 7.46.248.16>...
"Joel" <jo******@hotma il.com> wrote in
news:eI******** ******@TK2MSFTN GP15.phx.gbl:
> Run this method:
>
> public void test()
> {
> bool b;
> int i=0;
>
> b=false;
> i=0;
> b=(b && i++==1);
> Console.WriteLi ne(i.ToString() );
>
> b=false;
> i=0;
> b&=i++==1;
> Console.WriteLi ne(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_NO SPAM_Xcrtimmons inc.com> wrote in message
news:Xn******** *************** ***********@207 .46.248.16...
be*********@yah oo.com (Xarky) wrote in
news:bc******** *************** **@posting.goog le.com:
"Chris R. Timmons" <crtimmons@X_NO SPAM_Xcrtimmons inc.com> wrote
in message
news:<Xn******* *************** ************@20 7.46.248.16>...
"Joel" <jo******@hotma il.com> wrote in
news:eI******** ******@TK2MSFTN GP15.phx.gbl:

> Run this method:
>
> public void test()
> {
> bool b;
> int i=0;
>
> b=false;
> i=0;
> b=(b && i++==1);
> Console.WriteLi ne(i.ToString() );
>
> b=false;
> i=0;
> b&=i++==1;
> Console.WriteLi ne(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
3027
by: Mattias Campe | last post by:
Hi, Depending on if I get an image or a text of a certain URL, I want to do something different. I don't know in advance whether I'll get an image or a text. This is a URL that returns an image: http://indicator.amessage.info/indicator.php?param1=cobnet%40jabber.org&amp;param2=bounce&amp;param3=http%3A%2F%2Fstudent.ugent.be%2Fastrid%2Fpics%2Fjabber%2F&amp;param4=.png
3
1671
by: Stefan Weiss | last post by:
Hi. function printx($str) { echo $str; } true && printx("foo"); true && print("bar");
12
9448
by: Sammy | last post by:
Hi, my mind is going crazy. I have tried everything I can think of to no avail. I have tried Disable Output Escaping. I tried to think of a way of enclosing the attribute data in a CDATA element. That did not parse. Here is my question: How can I get attribute values to not get converted from &apos; to '
11
3617
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own rules when it comes to file paths. A lot of Microsoft installers for example, and also installers of other companies, do not work because they handle paths in the following manner:
2
5926
by: lf | last post by:
If I want to get a pointer to the initial data element in a vector<double>, does it make any difference whether I use (for vector<double> v) double* pVec1 = &v; or double* pVec2 = &v.front(); The intention is to pass the pointer to a function that expects such type. I vaguely remember someone recommended the second approach, but why?
16
2451
by: Steven T. Hatton | last post by:
In the following code, the only way I can figure out to pass an array of const is by setting the template argument to const in the instanciation expression. It would be (or seem to me) better if I could set that qualifier in the function call. Can that be done? #include <iostream> using std::ostream; using std::cout;
27
3820
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get failed assertions for assertions that at first thought ought to hold, but that's not important.) At the end is a full program containing the above seemingly
2
1332
by: Eric | last post by:
Came across a strange compiler difference today and wasn't sure which compiler had the bug. void AFunction( Rect &aRect ) { if ( 1 ) { Rect aRect = { aRect.top, aRect.left, aRect.bottom, aRect.right }; } }
3
2191
by: Tony | last post by:
I see that many pages have &amp; in querystring instead &. What is difference? Can I put page link (url) www.mysite.com/mypage.aspx?lang=EN&ID=15 or I need to write www.mysite.com/mypage.aspx?lang=EN&amp;ID=15 What is difference between & and &amp; and what is better?
0
7934
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8425
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8418
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5886
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5445
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3912
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1541
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1271
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.