boolean short circuit

M

Michael Jørgensen

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.
 
A

Artie Gold

Michael said:
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
 
U

usr.root

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.
 
M

Michael Jørgensen

Artie Gold said:
Bitwise and is not short-circuiting.

Thanks.

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

-Michael.
 
A

Antonio Contreras

Michael said:
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
 
J

Jack Klein

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top