Eg, if I write something like this
is it possible that b() doesn't get evaluated if a() returns false? What does the standard say about this?Code:if (a() && b()) { //..... }
Thank you
Eg, if I write something like this
is it possible that b() doesn't get evaluated if a() returns false? What does the standard say about this?Code:if (a() && b()) { //..... }
Thank you
Yes, that's right. If a() returns false, everything to the right of the && won't be executed.
The answer is: b() won't get evaluated if a() returns false.
I don't know what the standard say about, but this program will tell you:
Code:#include <iostream> using namespace std; bool a () { cout << "a\n"; return 0; } bool b () { cout << "b\n"; return 1; } int main () { if (a() && b()); return 0; }
hmm I see. Thanks for the help. I was just making sure that it is the standard behaviour.
btw, then, what should I write if I want all of them to be evaluated regardless of the return values? (except making a bunch of temporary variables and recording each return values first of course)
The standard says that b() will definitely not be evaluated if a() is false. Code which actually uses this behavior is not uncommon. For instance:
Will invoke the functions A, B, C, D, in that order, as long as they return true. You can use this to do a multi-part operation in sequence, stopping if anything fails. The alternative using if-statements would be:Code:A() && B() && C() && D();
The || operator can be used in the same way but the opposite sense.Code:if(A()) { if(B()) { if(C()) { D(); } } }
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
hmm I see... Thanks for the suggestions.
Note that this is GUARANTEED by the standard, and it allows things like this to work:
--Code:if (fgets(buffer, sizeof buffer, stdin) != NULL && buffer[0] != '\n') ... // or if (x && y / x > 7) ... // if x == 0, we can't divide by x. // or if (ptr != NULL && ptr->member == mymember) ...
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Of course, if you have a loop header, this might not work. You could use the comma operator instead.
Code:while(a_status = a(), b_status = b(), a_status && b_status)
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
There is a sequence point at the comma operator, so the order of evaluation is defined here.But couldn't those be evaluated in any order, or is that only for function parameters?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
because a() and b() have side effects.Why would you want to?
If you only want your if statement to execute if both functions return true, why waste time executing b() if a() already returned false?