expression execution order

This is a discussion on expression execution order within the C Programming forums, part of the General Programming Boards category; I'd like to call a number of functions, all which can only be called if the previous function succeeded. All ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    245

    expression execution order

    I'd like to call a number of functions, all which can only be called if the previous function succeeded. All functions return bool results, so just wondering what the best way to do this would be.

    I'm currently using the following method:

    Code:
    if (func_a())
       if (func_b())
          if (func_c())
    (etc...)
    but wondering if this would be better:

    Code:
    if (func_a() && func_b() && func_c())
    Problem is, would they all be called regardless of whether the previous one failed, or would execution stop when one of them returned FALSE? Or worse still, is the above unspecified, and depends on compiler?
    Last edited by _Elixia_; 10-02-2003 at 03:32 PM.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157

    Re: expression execution order

    Use your first method

    The second method may not work in all compilers (if any), the first method is easier to read, debug, and maintain.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    The evaluation order of && and || is defined, otherwise things like

    if ( p && *p != 0 )
    would randomly fail if p was a NULL pointer

    So
    if (func_a() && func_b() && func_c())
    would stop as soon as one of the funcs returned false, and those calls to the right would NOT happen.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    Ok, thanks. Much appreciated!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. order of execution of tokens in a c++ source code
    By sandy.sandipanc in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2008, 12:34 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 08:15 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. randomizing order of execution of function
    By y2jasontario in forum C Programming
    Replies: 2
    Last Post: 04-03-2002, 06:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21