13

Possible Duplicate:
Is short-circuiting boolean operators mandated in C/C++? And evaluation order?

Is there any defined by standard or math rules order of eveluating boolean sentences? For example:

if (firstTrue && secondTrue)
{
}

can I be sure that firstTrue will be checked first?

Community
  • 1
  • 1
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
  • 2
    Don´t take this harsh, but getting a copy of the standard (search for it in google) and looking up there can help with most of your questions. – David Rodríguez - dribeas Sep 25 '10 at 22:58
  • 2
    @David I'm ok, but instead of spendig God knows how long I can ask it here and I have an answer within 15 min max. That's why if I do not know precise place in to which I'm suppose to look for I ask question instead of searching for hours on end by reading standard. And you may say but standard is the precise place you should look in. Yes but this place is so huge that I do not wan't to spend as I've said hours on looking for something I'd rather ask question here and have as good answer in much shorter time. Do you agree that it does make sense? – There is nothing we can do Sep 26 '10 at 08:02
  • 3
    This is like someone being hungry and getting free fish. It solves the current problem, but won't teach you how to catch the fish yourself. The standard is hard to read at first, but after you get used to its organization browsing for an answer is usually fast. Specially with these type of very local questions. Without checking, I can guess that there are no more than two or three places where you can find the `&&` token in the document. – David Rodríguez - dribeas Sep 26 '10 at 09:55
  • 6
    @David No, it is like having characteristics of a professional programmer. If there is short and long way to do achieve a goal - prefer the shorter. – There is nothing we can do Sep 26 '10 at 16:41
  • 1
    continue Your example with fish is inappropriate. What do you mean it won't teach me how to catch fish? My fish was operator && not how to read and understand standard- I hope you agree on that. I've had two options to catch this fish (get the answer to my question about && operator) in either easy way or hard way. When you're hungry and someone would present you with either ready fried fish (answer to your hunger) or with fishing rod (standard with which help you to satisfy your hunger) which one would you prefer? You must realize that I didn't ask how to catch fish I just asked for a fish. – There is nothing we can do Sep 26 '10 at 16:44
  • 1
    @David my advice for you, when you want to visualise some abstract situation by real example you must be really carefull and you must fully understand what you are trying to visualize. – There is nothing we can do Sep 26 '10 at 16:46

3 Answers3

19

Yes. && and || are short circuiting operators. The order of evaluation of operands is well defined (left to right).

&& is also a sequence point.
So writing if( ++i && i) { } is perfectly fine.

ISO C++03 (5.14/1) says:

The && operator groups left-to-right. The operands are both implicitly converted to type bool (clause 4). The result is true if both operands are true and false otherwise. Unlike &, && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.

EDIT: (After seeing the comment)

ISO C++03 (Section 1.9/18) says

In the evaluation of each of the expressions

  • a && b
  • a || b
  • a ? b : c
  • a , b

    using the built-in meaning of the operators in these expressions (5.14, 5.15, 5.16, 5.18), there is a sequence point after the evaluation of the first expression.

  • Prasoon Saurav
    • 91,295
    • 49
    • 239
    • 345
    12

    Yes firstTrue will be evaluated first. In fact, if firstTrue is false, the secondTrue will not even be evaluated. This is called short circuiting.

    Check out this article: http://en.wikipedia.org/wiki/Short-circuit_evaluation

    The same happens with ||. If the first argument to || is true, the second will not be evaluated.

    Starkey
    • 9,673
    • 6
    • 31
    • 51
    1

    It is not about boolean sentences. It is about specific operators in these "sentences" (logical expressions, actually)

    The built-in && operator (as well as ||) are special: they guarantee that the left-hand side is evaluated before the right-hand size. They have a sequence point between the LHS and RHS evaluations. And they don't evaluate the RHS if the result is pre-determined by the LHS. Aside from this (and some other operators that have similar sequencing properties), there are no guarantees about the order of evaluation of logical expressions, or any other expressions.

    The above applies to built-in && and || operators. Overloaded && and || operators are not special in any way.

    AnT stands with Russia
    • 312,472
    • 42
    • 525
    • 765