short circuit evaluation

Discussion in 'C Programming' started by Lassie, Sep 16, 2008.

  1. Lassie

    Lassie Guest

    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 it get evaluated sometimes?
    Lassie, Sep 16, 2008
    #1
    1. Advertising

  2. "Lassie" <> writes:

    > if (finished && try_again()) { }
    >
    > Is my understanding of short circuit evaluation correct that if finished
    > is false that try_again() will never be executed?


    Yes.

    --
    Jean-Marc
    Jean-Marc Bourguet, Sep 16, 2008
    #2
    1. Advertising

  3. Lassie

    Guest

    On Sep 16, 6:55 pm, "Lassie" <> wrote:
    > 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 it get evaluated sometimes?



    No, it does not evaluate if finished is 0.

    Thus, this is correct:

    return 0 && 1 / 0;

    The expression (0 && 1 / 0) is evaluated like this:

    (a && b)

    First, a is evaluated. If a is false, then the whole expression is
    replaced by 0. (false)
    If a is true, then b is evaluated, and the expression is replaced by
    true or false, depending on what b was evaluated.

    This is also correct

    return 1 || 1 / 0;
    , Sep 16, 2008
    #3
  4. Lassie

    Richard Guest

    writes:

    > On Sep 16, 6:55 pm, "Lassie" <> wrote:
    >> 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 it get evaluated sometimes?

    >
    >
    > No, it does not evaluate if finished is 0.
    >
    > Thus, this is correct:
    >
    > return 0 && 1 / 0;
    >
    > The expression (0 && 1 / 0) is evaluated like this:
    >
    > (a && b)
    >
    > First, a is evaluated. If a is false, then the whole expression is
    > replaced by 0. (false)
    > If a is true, then b is evaluated, and the expression is replaced by
    > true or false, depending on what b was evaluated.
    >
    > This is also correct
    >
    > return 1 || 1 / 0;
    >


    Wow what a long winded way of explaining something.

    Short answer : as soon as one "false" is matched from left to right the
    && statement "exits" and returns false.
    Richard, Sep 16, 2008
    #4
  5. Lassie

    Guest

    On Sep 16, 8:55 pm, "Lassie" <> wrote:
    > 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 it get evaluated sometimes?


    no it will not be evaluated ever,
    but are you trying it in "C".
    In "C" there is no "bool" built in data type .
    If you type casted "bool" as some other built in data type then please
    post as much relevant code as possible in queries in future

    --
    vIpIn
    , Sep 16, 2008
    #5
  6. In article <gaom77$gm1$>,
    Richard <> wrote in regards to our friend vippy:
    ....
    >Wow what a long winded way of explaining something.


    That is the way - if you are a reg wannabee.

    >Short answer : as soon as one "false" is matched from left to right the
    >&& statement "exits" and returns false.


    Another way:
    && means "evaluate while true [*]; return last evaluated expression"
    || means "evaluate until true [*]; return last evaluated expression"

    [*] Or end of list reached.
    Kenny McCormack, Sep 16, 2008
    #6
  7. Lassie

    Guest

    On Sep 16, 7:23 pm, wrote:
    > On Sep 16, 8:55 pm, "Lassie" <> wrote:
    >
    > > 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 it get evaluated sometimes?

    >
    > no it will not be evaluated ever,
    > but are you trying it in "C".
    > In "C" there is no "bool" built in data type .
    > If you type casted "bool" as some other built in data type then please
    > post as much relevant code as possible in queries in future


    There's a bool macro in <stdbool.h> in C99.
    , Sep 16, 2008
    #7
  8. Lassie

    Guest

    wrote:
    > On Sep 16, 8:55�pm, "Lassie" <> wrote:
    > > 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 it get evaluated sometimes?

    >
    > no it will not be evaluated ever,
    > but are you trying it in "C".
    > In "C" there is no "bool" built in data type .


    No, but it is a standard typedef. This is just a code fragment, which
    doesn't even provide declarations for is_done() and try_again(). I
    would assume that the missing code also contains a line which says

    #include <stdbool.h>

    > If you type casted "bool" as some other built in data type then please
    > post as much relevant code as possible in queries in future


    Agreed: explaining a problem by providing a complete program that
    demonstrates it always makes for more productive responses.
    , Sep 16, 2008
    #8
  9. Richard<> writes:
    > writes:
    >
    >> On Sep 16, 6:55 pm, "Lassie" <> wrote:
    >>> 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 it get evaluated sometimes?

    >>
    >>
    >> No, it does not evaluate if finished is 0.
    >>
    >> Thus, this is correct:
    >>
    >> return 0 && 1 / 0;
    >>
    >> The expression (0 && 1 / 0) is evaluated like this:
    >>
    >> (a && b)
    >>
    >> First, a is evaluated. If a is false, then the whole expression is
    >> replaced by 0. (false)
    >> If a is true, then b is evaluated, and the expression is replaced by
    >> true or false, depending on what b was evaluated.
    >>
    >> This is also correct
    >>
    >> return 1 || 1 / 0;
    >>

    >
    > Wow what a long winded way of explaining something.
    >
    > Short answer : as soon as one "false" is matched from left to right the
    > && statement "exits" and returns false.


    It's helpful to use correct terminology.

    && is not a statement, it's an operator, and it can be part of an
    expression. Since it's not a function it doesn't "return"
    anything; expressions *yield* values (though it's common to say that
    expressions return values, and it usually doesn't cause too much
    confusion). Your wording could be taken to imply that a && b && c is
    evaluated as if it were an && expression with 3 operands; in a sense
    it works that way, but it's actually built up from two 2-operand
    && subexpressions, equivalent to (a && b) && c. It's important
    to understand how expressions are built up from subexpressions;
    failing to understand this can easily lead to errors like
    0 <= x <= 100 (which is valid, but doesn't mean what one might
    assume). (Richard, I'm sure you know what 0 <= x <= 100 means.)

    So ...

    a && b yields a true value (1) if both of its operands are true
    (non-zero); otherwise it yields a false value (0). If the first
    operand is false, the second operand is not evaluated. The two
    operands are evaluated in sequence; if the second operand is
    evaluated this happens *after* the first operand is evaluated.

    a || b yields a true value (1) if either of its operands is true
    (non-zero); otherwise it yields a false value (0). If the first
    operand is true, the second operand is not evaluated. The two
    operands are evaluated in sequence; if the second operand is
    evaluated this happens *after* the first operand is evaluated.

    As with 2-operand operator, && expressions can be chained together,
    as in a && b && c && d, which is equivalent to ((a && b) && c) && d.
    This yields 1 if all of a, b, c, or d are true; otherwise it
    yields 0. a, b, c, and d are evaluated in strict left-to-right
    order; if any of them is false, the rest are not evaluated.
    || can be chained similarly. There are not special-case rules;
    they follow from the rules for && and || (including the fact that
    they're left-associative).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 16, 2008
    #9
  10. Lassie

    Guest

    On Sep 16, 7:38 pm, wrote:
    > wrote:
    > > On Sep 16, 8:55 pm, "Lassie" <> wrote:
    > > > 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 it get evaluated sometimes?

    >
    > > no it will not be evaluated ever,
    > > but are you trying it in "C".
    > > In "C" there is no "bool" built in data type .

    >
    > No, but it is a standard typedef. This is just a code fragment, which
    > doesn't even provide declarations for is_done() and try_again(). I
    > would assume that the missing code also contains a line which says
    >
    > #include <stdbool.h>


    It is actually a macro, it's explicity mentioned in the standard to be
    so.
    See 7.16 p 2
    , Sep 16, 2008
    #10
  11. writes:
    > wrote:
    >> On Sep 16, 8:55�pm, "Lassie" <> wrote:
    >> > 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 it get evaluated sometimes?

    >>
    >> no it will not be evaluated ever,
    >> but are you trying it in "C".
    >> In "C" there is no "bool" built in data type .

    >
    > No, but it is a standard typedef. This is just a code fragment, which
    > doesn't even provide declarations for is_done() and try_again(). I
    > would assume that the missing code also contains a line which says
    >
    > #include <stdbool.h>


    Either that, or it was compiled with a C++ compiler.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 16, 2008
    #11
  12. Lassie

    Ben Pfaff Guest

    (Kenny McCormack) writes:

    > && means "evaluate while true [*]; return last evaluated expression"
    > || means "evaluate until true [*]; return last evaluated expression"


    That is not completely correct. The result of && is always 0 or
    1. It is not, say, 2, even if the last evaluated subexpression
    evaluates to 2.
    --
    "Some people *are* arrogant, and others read the FAQ."
    --Chris Dollin
    Ben Pfaff, Sep 16, 2008
    #12
  13. In article <>,
    Ben Pfaff <> wrote:
    > (Kenny McCormack) writes:
    >
    >> && means "evaluate while true [*]; return last evaluated expression"
    >> || means "evaluate until true [*]; return last evaluated expression"

    >
    >That is not completely correct. The result of && is always 0 or
    >1. It is not, say, 2, even if the last evaluated subexpression
    >evaluates to 2.


    That's true. I could have said "return the truth value of the last
    evaluated expression" - but I wanted to fit it into an 80 character
    line.

    Of course, the phrase "truth value" probably doesn't occur in the holy
    standards documents, so, no doubt, somebody will come along and nit pick
    that.
    Kenny McCormack, Sep 16, 2008
    #13
  14. Lassie

    Ben Pfaff Guest

    (Kenny McCormack) writes:

    > In article <>,
    > Ben Pfaff <> wrote:
    >> (Kenny McCormack) writes:
    >>
    >>> && means "evaluate while true [*]; return last evaluated expression"
    >>> || means "evaluate until true [*]; return last evaluated expression"

    >>
    >>That is not completely correct. The result of && is always 0 or
    >>1. It is not, say, 2, even if the last evaluated subexpression
    >>evaluates to 2.

    >
    > That's true. I could have said "return the truth value of the last
    > evaluated expression" - but I wanted to fit it into an 80 character
    > line.


    I suppose you could say "return !!(last evaluated expression)" or
    "return (last evaluated expression) != 0". Either way, I guess
    that it is not necessarily clear to anyone who doesn't already
    understand what's going on.
    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
    Ben Pfaff, Sep 16, 2008
    #14
  15. Lassie

    CBFalconer Guest

    wrote:
    > "Lassie" <> wrote:
    >
    >> 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 it get evaluated sometimes?

    >
    > no it will not be evaluated ever, but are you trying it in "C".
    > In "C" there is no "bool" built in data type. If you type casted
    > "bool" as some other built in data type then please post as much
    > relevant code as possible in queries in future


    Wrong. It is available in C99, provided you #include <stdbool.h>.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, Sep 16, 2008
    #15
  16. CBFalconer <> wrote:
    > wrote:
    > > In "C" there is no "bool" built in data type. ...

    >
    > Wrong.  It is available in C99, provided you #include
    > <stdbool.h>.


    Wrong. With or without <stdbool.h>, bool is not a built
    in data type in C99.

    #include <stdio.h>
    #include <stdbool.h>

    #undef bool

    int main(void)
    {
    int bool = 42;
    printf("%d\n", bool);
    return 0;
    }

    --
    Peter
    Peter Nilsson, Sep 16, 2008
    #16
  17. Lassie

    Richard Guest

    Peter Nilsson <> writes:

    > CBFalconer <> wrote:
    >> wrote:
    >> > In "C" there is no "bool" built in data type. ...

    >>
    >> Wrong.  It is available in C99, provided you #include
    >> <stdbool.h>.

    >
    > Wrong. With or without <stdbool.h>, bool is not a built
    > in data type in C99.
    >
    > #include <stdio.h>
    > #include <stdbool.h>
    >
    > #undef bool
    >
    > int main(void)
    > {
    > int bool = 42;
    > printf("%d\n", bool);
    > return 0;
    > }


    I'm not quite sure what your example is supposed to prove but my gcc
    stdbool includes this comment:

    /*
    * ISO C Standard: 7.16 Boolean type and values <stdbool.h>
    */

    And it goes on to define bool.
    Richard, Sep 17, 2008
    #17
  18. blargg <> writes:

    > In article <gaopl6$ek$>,
    > (Kenny McCormack) wrote:
    >
    >> In article <>,
    >> Ben Pfaff <> wrote:
    >> > (Kenny McCormack) writes:
    >> >
    >> >> && means "evaluate while true [*]; return last evaluated expression"
    >> >> || means "evaluate until true [*]; return last evaluated expression"
    >> >
    >> >That is not completely correct. The result of && is always 0 or
    >> >1. It is not, say, 2, even if the last evaluated subexpression
    >> >evaluates to 2.

    >>
    >> That's true. I could have said "return the truth value of the last
    >> evaluated expression" - but I wanted to fit it into an 80 character
    >> line.

    >
    > How about this?
    >
    > a && b means a ? !!b : 0
    > a || b means a ? 1 : !!b


    Yes, and it even has the sequence points in the right places. But (it
    would not be clc without a "but" would it?) it involves even more to
    learn, so it is not ideal as an explanation.

    The standard gets knocked (by some) as wordy and obscure but, it does
    very well here:

    The && operator shall yield 1 if both of its operands compare
    unequal to 0; otherwise, it yields 0. The result has type int.

    Unlike the bitwise binary & operator, the && operator guarantees
    left-to-right evaluation; there is a sequence point after the
    evaluation of the first operand. If the first operand compares equal
    to 0, the second operand is not evaluated.

    --
    Ben.
    Ben Bacarisse, Sep 17, 2008
    #18
  19. pete <> writes:
    > Keith Thompson wrote:
    >> && is not a statement, it's an operator, and it can be part of an
    >> expression. Since it's not a function it doesn't "return"
    >> anything; expressions *yield* values (though it's common to say that
    >> expressions return values, and it usually doesn't cause too much
    >> confusion).

    >
    > You can find this phrase: "These operators return values",
    > in the standard.


    Yes, in C99 6.5p4.

    This is changed to "These operators yield values" in both n1124
    (C99+TC1+TC2) and n1256 (C99+TC1+TC2+TC3).

    n1336.pdf, the very early first draft of C201x, also uses "yield".

    Oddly, I can't find this change either in any of the Technical
    Corrigenda or in any of the Defect Reports.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 17, 2008
    #19
  20. Richard wrote:
    > Peter Nilsson <> writes:
    >
    >> CBFalconer <> wrote:
    >>> wrote:
    >>>> In "C" there is no "bool" built in data type. ...
    >>>
    >>> Wrong. It is available in C99, provided you #include
    >>> <stdbool.h>.

    >>
    >> Wrong. With or without <stdbool.h>, bool is not a built
    >> in data type in C99.
    >>
    >> #include <stdio.h>
    >> #include <stdbool.h>
    >>
    >> #undef bool
    >>
    >> int main(void)
    >> {
    >> int bool = 42;
    >> printf("%d\n", bool);
    >> return 0;
    >> }

    >
    > I'm not quite sure what your example is supposed to prove but my gcc
    > stdbool includes this comment:
    >
    > /*
    > * ISO C Standard: 7.16 Boolean type and values <stdbool.h>
    > */
    >
    > And it goes on to define bool.


    Like this:
    #define bool _Bool

    So it is true: C does not have a builtin type bool, C99 does have a builtin
    type _Bool. C99 does have a macro bool in <stdbool.h>.

    From n1256
    6.2.5.2:
    An onject declared as type _Bool is large enough to store the values 0 and
    1.

    6.3.1.1:
    The rank of _Bool shall be less than the rank of all other standard integer
    types.

    6.7.2.1 and .2, "type specifiers" mentions _Bool, but not bool

    7.16.2:
    The macro bool expands to _Bool.

    Bye, Jojo
    Joachim Schmitz, Sep 17, 2008
    #20
    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:
    727
    Joona I Palaste
    Oct 23, 2003
  2. webposter
    Replies:
    2
    Views:
    650
    Peter Shaggy Haywood
    Sep 14, 2004
  3. Anthony Paul

    short-circuit evaluation and assignment operators

    Anthony Paul, Jun 6, 2009, in forum: C Programming
    Replies:
    5
    Views:
    1,284
  4. mingze zhang
    Replies:
    2
    Views:
    465
    James Kanze
    Jul 15, 2010
  5. mingze zhang
    Replies:
    4
    Views:
    1,118
    aadilsabri
    Oct 22, 2011
Loading...

Share This Page