boolean short circuit

Discussion in 'C Programming' started by Michael Jørgensen, Sep 22, 2005.

  1. 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.
     
    Michael Jørgensen, Sep 22, 2005
    #1
    1. Advertising

  2. Michael Jørgensen

    Artie Gold Guest

    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!"
     
    Artie Gold, Sep 22, 2005
    #2
    1. Advertising

  3. Michael Jørgensen

    Guest

    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.
     
    , Sep 22, 2005
    #3
  4. "Artie Gold" <> wrote in message
    news:...
    > 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.
     
    Michael Jørgensen, Sep 22, 2005
    #4
  5. 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
     
    Antonio Contreras, Sep 22, 2005
    #5
  6. Michael Jørgensen

    Jack Klein Guest

    On 21 Sep 2005 22:40:41 -0700, ""
    <> 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/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Sep 23, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil...
    Replies:
    10
    Views:
    757
    Joona I Palaste
    Oct 23, 2003
  2. Phil...
    Replies:
    1
    Views:
    994
    Thomas G. Marshall
    Oct 22, 2003
  3. JS#
    Replies:
    5
    Views:
    3,567
    Roedy Green
    Nov 21, 2005
  4. webposter
    Replies:
    2
    Views:
    709
    Peter Shaggy Haywood
    Sep 14, 2004
  5. Replies:
    0
    Views:
    617
Loading...

Share This Page