# Thread: Order of operations question

1. ## 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

In the previous example, there are several sub-expressions, including
and
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. By short circuiting, it means here that: if username is not "root", the whole statement returns false, end of story.

3. 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.

4. Thank you both for your time, makes total sense!