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

boolean short circuit

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


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

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

P: n/a

"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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.