Velocity Reviews > boolean short circuit

# boolean short circuit

Michael Jørgensen
Guest
Posts: n/a

 09-22-2005
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.

Artie Gold
Guest
Posts: n/a

 09-22-2005
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!"

usr.root@gmail.com
Guest
Posts: n/a

 09-22-2005
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.

Michael Jørgensen
Guest
Posts: n/a

 09-22-2005

"Artie Gold" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.

Antonio Contreras
Guest
Posts: n/a

 09-22-2005
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

Jack Klein
Guest
Posts: n/a

 09-23-2005
On 21 Sep 2005 22:40:41 -0700, "(E-Mail Removed)"
<(E-Mail Removed)> 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?

>

> 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