First statement always evaluated first?

Discussion in 'C Programming' started by tconkling, Jul 9, 2005.

  1. tconkling

    tconkling Guest

    I have an if statement that looks like this:

    if(foo(&x) && x > y)
    ...

    where the value of x is modified by foo, and the comparison between x
    and y only makes sense after x has been modified by foo (and, of
    course, if foo returns true). Am I guaranteed (assuming my compiler
    generates correct code) that x > y is evaluated after foo(&x) returns?

    Assuming things work the way I think they do, is it considered bad form
    to write code like this? It saves me from doing something like the
    following, which I think is ugly-looking:

    if(foo(&x))
    {
    if(x > y)
    {
    ...
    }
    }

    Thanks,
    Tim
    tconkling, Jul 9, 2005
    #1
    1. Advertising

  2. tconkling

    Sensei Guest

    tconkling wrote:
    > I have an if statement that looks like this:
    >
    > if(foo(&x) && x > y)
    > ...
    >
    > where the value of x is modified by foo, and the comparison between x
    > and y only makes sense after x has been modified by foo (and, of
    > course, if foo returns true). Am I guaranteed (assuming my compiler
    > generates correct code) that x > y is evaluated after foo(&x) returns?


    Yes in some sense. The order of evaluation is lexicographic, so first

    foo(&x)

    and *IF* foo is true then is (x > y) evaluated, otherwise it goes rigth
    to the else statement.

    > Assuming things work the way I think they do, is it considered bad form
    > to write code like this? It saves me from doing something like the
    > following, which I think is ugly-looking:
    >
    > if(foo(&x))
    > {
    > if(x > y)
    > {
    > ...
    > }
    > }


    They are equivalent.
    Sensei, Jul 9, 2005
    #2
    1. Advertising

  3. tconkling

    Tim Prince Guest

    tconkling wrote:
    > I have an if statement that looks like this:
    >
    > if(foo(&x) && x > y)
    > ...
    >
    > where the value of x is modified by foo, and the comparison between x
    > and y only makes sense after x has been modified by foo (and, of
    > course, if foo returns true). Am I guaranteed (assuming my compiler
    > generates correct code) that x > y is evaluated after foo(&x) returns?
    >
    > Assuming things work the way I think they do, is it considered bad form
    > to write code like this? It saves me from doing something like the
    > following, which I think is ugly-looking:
    >
    > if(foo(&x))
    > {
    > if(x > y)
    > {
    > ...
    > }
    > }
    >
    > Thanks,
    > Tim
    >

    Yes, those forms are equivalent. Of course, you don't need the braces
    around the inner if().
    Tim Prince, Jul 9, 2005
    #3
  4. "tconkling" <> writes:
    > I have an if statement that looks like this:
    >
    > if(foo(&x) && x > y)
    > ...
    >
    > where the value of x is modified by foo, and the comparison between x
    > and y only makes sense after x has been modified by foo (and, of
    > course, if foo returns true). Am I guaranteed (assuming my compiler
    > generates correct code) that x > y is evaluated after foo(&x) returns?
    >
    > Assuming things work the way I think they do, is it considered bad form
    > to write code like this? It saves me from doing something like the
    > following, which I think is ugly-looking:
    >
    > if(foo(&x))
    > {
    > if(x > y)
    > {
    > ...
    > }
    > }


    Yes, that's a special property of the "&&" operator (also "||" and
    ","). The "&&" operator evaluates its left operand first, then
    evaluates the right operand only of the left operand evaluted to a
    non-zero value. There's also a sequence point between the evaluation
    of the left and right operands.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jul 9, 2005
    #4
    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. Steph
    Replies:
    2
    Views:
    1,659
    heislord5
    May 11, 2011
  2. Jay McGavren
    Replies:
    11
    Views:
    1,127
    Alan Krueger
    Jan 16, 2006
  3. Deryck
    Replies:
    4
    Views:
    515
    derek giroulle
    Jun 22, 2004
  4. William Payne
    Replies:
    3
    Views:
    277
    William Payne
    Apr 9, 2004
  5. Robin Wenger
    Replies:
    3
    Views:
    392
    Arne Vajhøj
    Jan 20, 2011
Loading...

Share This Page