473,513 Members | 2,456 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 = SomeFunctionReturningFalse();
success &= SomeOtherFunction();

and

bool success = SomeFunctionReturningFalse();
success = success && SomeOtherFunction().

????

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

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

Is there any difference between

bool success = SomeFunctionReturningFalse();
success &= SomeOtherFunction();

and

bool success = SomeFunctionReturningFalse();
success = success && SomeOtherFunction().

????

In the latter case I expect that SomeOtherFunction() 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
someotherFunction() 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*******@austin.rr.com> wrote in message
news:3p************@individual.net...
Michael Jørgensen wrote:
Hi there,

Is there any difference between

bool success = SomeFunctionReturningFalse();
success &= SomeOtherFunction();

and

bool success = SomeFunctionReturningFalse();
success = success && SomeOtherFunction().

????

In the latter case I expect that SomeOtherFunction() 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 = SomeFunctionReturningFalse();
success &= SomeOtherFunction();

and

bool success = SomeFunctionReturningFalse();
success = success && SomeOtherFunction().

????

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


In the first case SomeOtherFunction() 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 = SomeFunctionReturningFalse();
success &= SomeOtherFunction();

and

bool success = SomeFunctionReturningFalse();
success = success && SomeOtherFunction().

????

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


And here is your answer:
someotherFunction() 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.learn.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
3901
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...
3
3795
by: Steven Wong | last post by:
Just wondering, is it portable to write: something* pSomething = getSomething(); if( pSomething && pSomething->stuff() ) { .... }
2
4112
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...
6
1606
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....
5
3631
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...
33
2522
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...
7
1735
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 ...
0
1808
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
2811
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...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7099
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...
0
7525
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...
0
5685
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,...
1
5086
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...
0
4746
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...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
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...

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.