Like Tree1Likes
  • 1 Post By grumpy

Relying on order of execution

This is a discussion on Relying on order of execution within the C Programming forums, part of the General Programming Boards category; Is it bad practice to assume the proper (left to right) execution of boolean expressions for simplicity? Reliant code: Code: ...

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92

    Relying on order of execution

    Is it bad practice to assume the proper (left to right) execution of boolean expressions for simplicity?

    Reliant code:
    Code:
    if(
      !Is_Next_Match(Parser, TOKEN_WEIGHT)                                           ||
      !Next_Number(Parser, j, INT)                                                   ||
      j != i                                                                         ||
      !Next_Number(Parser, Character->Meshes[Mesh_Index]->Weights[i]->Joint          ||
      !Next_Number(Parser, Character->Meshes[Mesh_Index]->Weights[i]->Bias           ||
      !Next_Number(Parser, Character->Meshes[Mesh_Index]->Weights[i]->Position[_X_]) ||
      !Next_Number(Parser, Character->Meshes[Mesh_Index]->Weights[i]->Position[_Y_]) ||
      !Next_Number(Parser, Character->Meshes[Mesh_Index]->Weights[i]->Position[_Z_])
    )
      goto Error_Currupt_File;
    Non-reliant code:
    Code:
    if(!Is_Next_Match(Parser, TOKEN_WEIGHT))
      goto Error_Currupt_File;
    if(!Next_Number(Parser, j, INT))
      goto Error_Currupt_File;
    ....

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    No, it is perfectly fine, but be careful not to switch || for | otherwise you lose the sequence point introduced.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    Well if you can't rely on that, your compiler is broken.
    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
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    Excellent, thanks for the clarification

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    && and || are fast fail, so you can rely on them not only to execute the their operands in order, but to not execute the second argument when the result is determined by the first. ?: is similar too.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,440
    Quote Originally Posted by King Mir View Post
    && and || are fast fail, so you can rely on them not only to execute the their operands in order, but to not execute the second argument when the result is determined by the first.
    A more usual description is "short-circuit evaluation".
    Salem likes this.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. order of execution
    By manzoor in forum C++ Programming
    Replies: 15
    Last Post: 08-13-2009, 01:14 PM
  2. Order of execution of preprocessor macros
    By DL1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 06:52 PM
  3. expression execution order
    By _Elixia_ in forum C Programming
    Replies: 3
    Last Post: 10-02-2003, 04:01 PM
  4. uncertainty in order of execution - any explanation?
    By ylzhang in forum C Programming
    Replies: 6
    Last Post: 05-31-2003, 08:04 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