473,587 Members | 2,508 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

boolean short circuit

Hi there,

Is there any difference between

bool success = SomeFunctionRet urningFalse();
success &= SomeOtherFuncti on();

and

bool success = SomeFunctionRet urningFalse();
success = success && SomeOtherFuncti on().

????

In the latter case I expect that SomeOtherFuncti on() will not be called,
because of short circuit evaluation, but what about the first case?

-Michael.
Nov 15 '05 #1
5 1907
Michael Jørgensen wrote:
Hi there,

Is there any difference between

bool success = SomeFunctionRet urningFalse();
success &= SomeOtherFuncti on();

and

bool success = SomeFunctionRet urningFalse();
success = success && SomeOtherFuncti on().

????

In the latter case I expect that SomeOtherFuncti on() will not be called,
because of short circuit evaluation, but what about the first case?


Bitwise and is not short-circuiting.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Nov 15 '05 #2
someotherFuncti on() will always be called .
in the first case maybe the success has relationship with both the two
functions.
if they both return ture or both return false ,success is set with true
..other case it set false.

Nov 15 '05 #3

"Artie Gold" <ar*******@aust in.rr.com> wrote in message
news:3p******** ****@individual .net...
Michael Jørgensen wrote:
Hi there,

Is there any difference between

bool success = SomeFunctionRet urningFalse();
success &= SomeOtherFuncti on();

and

bool success = SomeFunctionRet urningFalse();
success = success && SomeOtherFuncti on().

????

In the latter case I expect that SomeOtherFuncti on() will not be called,
because of short circuit evaluation, but what about the first case?


Bitwise and is not short-circuiting.


Thanks.

I had overlooked that "&=" is "bitwise and". On ther other hand, there is no
"&&=" operator.

-Michael.
Nov 15 '05 #4
Michael Jørgensen wrote:
Hi there,

Is there any difference between

bool success = SomeFunctionRet urningFalse();
success &= SomeOtherFuncti on();

and

bool success = SomeFunctionRet urningFalse();
success = success && SomeOtherFuncti on().

????

In the latter case I expect that SomeOtherFuncti on() will not be called,
because of short circuit evaluation, but what about the first case?


In the first case SomeOtherFuncti on() will allways be called. There are
other differences. 'Bitwise and' and 'logic and' are different
operators. If you want a logic and, you should use it. For example,
given the following declaration and assuming a 2's complement
representation.

int a = 2, b = 4;

a && b == 1 //both values represent true
a & b == 0 //0b00...0010 & 0b00...0100 = 0b00...0000

Nov 15 '05 #5
On 21 Sep 2005 22:40:41 -0700, "us******@gmail .com"
<us******@gmail .com> wrote in comp.lang.c:

Do not reply without quoting. Here is the OP's question:
Michael Jørgensen wrote:
Hi there,

Is there any difference between

bool success = SomeFunctionRet urningFalse();
success &= SomeOtherFuncti on();

and

bool success = SomeFunctionRet urningFalse();
success = success && SomeOtherFuncti on().

????

In the latter case I expect that SomeOtherFuncti on() will not be called,
because of short circuit evaluation, but what about the first case?


And here is your answer:
someotherFuncti on() will always be called .
in the first case maybe the success has relationship with both the two
functions.
if they both return ture or both return false ,success is set with true
.other case it set false.


As before, your answer is completely wrong. In both of the sample
cases, success is set to 0 unless BOTH of the functions return a
non-zero value.

In neither case is success set to 1 if both functions return zero.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
3914
by: Dave Opstad | last post by:
In this snippet: d = {'x': 1} value = d.get('x', bigscaryfunction()) the bigscaryfunction is always called, even though 'x' is a valid key. Is there a "short-circuit" version of get that doesn't evaluate the second argument if the first is a valid key? For now I'll code around it, but this behavior surprised me a bit...
3
3804
by: Steven Wong | last post by:
Just wondering, is it portable to write: something* pSomething = getSomething(); if( pSomething && pSomething->stuff() ) { .... }
2
4118
by: webposter | last post by:
Hi, I am looking for information on a data structure (and associated algorithm) to do short-circuit evaluation of boolean expressions and haven't found a single one even after googing for two days! Can anyone point me to good resources (or implementations) that do this. Basically is there any way to optimize a boolean expression expressed...
6
1614
by: James Curran | last post by:
In recent weeks, I've twice come upon people who consider the short- curcuited evaluation of logical && (and ||) to be just a vendor- defined compiler quirk, and recommend against depending on it. To help me reason with these people, can anyone here: 1) Verify that short-curcuit evaluation is specified in the orginal K&R "C Programming...
5
3633
by: Railgunner | last post by:
I am looking for a routine that can evaluate boolean expressions like: IF/ELSE-IF (X1=X2 AND (Y=1 OR Z1=Z2)) IF/ELSE-IF (A$='Y' AND B=1) OR (C=1 AND D>E) etc. I can handle determining if the individual terms (X1=X2, etc) are TRUE/FALSE at runtime. I just need the routine that determines if the entire expression is TRUE/FALSE...
33
2540
by: Stef Mientki | last post by:
hello, I discovered that boolean evaluation in Python is done "fast" (as soon as the condition is ok, the rest of the expression is ignored). Is this standard behavior or is there a compiler switch to turn it on/off ? thanks, Stef Mientki
7
1737
by: Flavio | last post by:
Hi, I have been playing with set operations lately and came across a kind of surprising result given that it is not mentioned in the standard Python tutorial: with python sets, intersections and unions are supposed to be done like this: In :set('casa') & set('porca') Out:set() In :set('casa') | set('porca')
0
1813
by: Greg Nash | last post by:
G'day I have a before-update trigger a bit like .... REFERENCING OLD AS O NEW AS N .... WHEN (N.STATUS 0 AND EXISTS (SELECT EXTREF FROM MY.ET WHERE EXTREF=N.REF OR EXTREF=O.REF))
30
2850
by: Lassie | last post by:
bool finished = is_done(); if (finished && try_again()) { } Is my understanding of short circuit evaluation correct that if finished is false that try_again() will never be executed? Or does it get evaluated sometimes?
0
7852
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...
1
7974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5719
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...
0
5395
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...
0
3845
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...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1192
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...

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.