Thread: Order of operations question

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    13

    Order of operations question

    In the book Jumping into C++ the author states that when doing comparisons between expressions (x != 0 && 10/x <5) the first comparison if returned false would short circuit the comparison stopping any chance of having a dived by 0 occurrence....

    the author then goes on to state

    Order of evaluation

    if(username == "root" && password == "xyzzy")

    In the previous example, there are several sub-expressions, including
    username == "root"
    and
    password == "xyzzy"
    Both of these expressions are evaluated before the Boolean operator.
    end quote


    I
    am confused, if the above statement about short circuiting is true how can the 2nd statement made by the author also be true? I thought the evaluations were done sequentially and tested and only if the 1st is true would the 2nd be evaluated...

    Any assistance or clarification would be most appreciated.

    Jonathan



  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    By short circuiting, it means here that: if username is not "root", the whole statement returns false, end of story.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    In the second case I believe the author is referring to the fact that you can't logically perform an 'and' operation (as per a truth table) on two things until you know whether those two things themselves are true or false.

    For the first on on the other hand, with the left hand argument being false, there is no need to evaluate the second argument and then no need to evaluate the result of anding the results from the left and right side.

    So they don't entirely disagree, it's perhaps just a really manky explanation because by the time it's evaluated both sides, it knows that the first one was true or it would not have gone that far. Thus the remaining result at that point is the same as the right hand side, and it doesn't technically really still need to perform an 'and' operation (as per a truth-table) at that point.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    13
    Thank you both for your time, makes total sense!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Order of operations question
    By GuitarNinja in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2009, 06:14 AM
  2. Order of operations question
    By nicomp in forum C++ Programming
    Replies: 9
    Last Post: 09-20-2006, 07:18 PM
  3. Order of Operations Question
    By chix/w/guns in forum C++ Programming
    Replies: 35
    Last Post: 07-17-2004, 03:38 AM
  4. order of operations...
    By jverkoey in forum C++ Programming
    Replies: 2
    Last Post: 05-21-2004, 11:38 PM
  5. order of operations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 10-31-2002, 08:51 PM