Evaluation order of multiple conditions inside "if" clause [duplicate] - c++

This question already has answers here:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 6 years ago.
I was trying to find the order of evaluation inside if clause when more than 2 conditions are specified. I found multiple results to talk about how 2 conditions are evaluated. My questions is when I have something like,
it=seqMap.find(a);
if( a !="" && it==seqMap.end() || isEven )
{
//Do something
}
I understand that this is not the right way to write code and braces are important but I am trying to understand how this will work, out of curiosity.

The built-in boolean operators impose a strict sequencing. They have shortcut evaluation where the second argument is not even evaluated if the first one determines the outcome. This is important in cases where evaluation of the second argument could have Undefined Behavior.
User defined boolean operators do not, as a rule, provide shortcut evaluation.
It's possible to emulate the effect but C++ doesn't really support this. C# does.
A && B || C
… is parsed as
(A && B) || C
… because && effectively has higher precedence than ||.
However, the C++ grammar is not defined in terms of precedence. Rather it's designed to emulate a precedence, which then appears as an emergent feature of the grammar. And this means that a naïve precedence view in some cases can indicate an incorrect parse, so use precedence reasoning with care.

It will first evaluate
a !="" && it==seqMap.end()
If a !="" evaluates to false then it will check
a !=""|| isEven
Otherwise if a !="" evaluates to true then, it==seqMap.end() will be evaluated and
finally the the answer of a !="" && it==seqMap.end() will be OR'ed with isEven

In C++ the logical AND operator has higher precedene than the logical OR operator.
Also the logical AND and OR operators have shortcut evaluation. If the first condition of AND is false, then the second condition will not be evaluated. Similarly, if the first condition of OR is true, then the second condition will not be evaluated.
For further reference on operator precedene:
http://en.cppreference.com/w/cpp/language/operator_precedence

Related

C++ if operator - OR, AND: testing methodology [duplicate]

This question already has answers here:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 5 years ago.
When encountering a (bool1 && bool2), does c++ ever attempts to check bool2 if bool1 was found false or does it ignore it the way PHP does?
Sorry if it is too basic of a question, but I really could not find a mentioning of that neither in Schildt nor on the Internet.
Yes, the && operator in C++ uses short-circuit evaluation so that if bool1 evaluates to false it doesn't bother evaluating bool2.
"Short-circuit evaluation" is the fancy term that you want to Google and look for in indexes.
The same happens with the || operator, if bool1 evaluates to true then the whole expression will evaluate to true, without evaluating bool2.
In case you want to evaluate all expressions anyway you can use the & and | operators.
C++ does use short-circuit logic, so if bool1 is false, it won't need to check bool2.
This is useful if bool2 is actually a function that returns bool, or to use a pointer:
if ( pointer && pointer->someMethod() )
without short-circuit logic, it would crash on dereferencing a NULL pointer, but with short-circuit logic, it works fine.
That is correct (short-cicuit behavior). But beware: short-circuiting stops if the operator invoked is not the built-in operator, but a user-defined operator&& (same with operator||).
Reference in this SO
The && operator short circuits in C++ - if bool1 was false in your example, bool2 wouldn't be checked/executed.
This is called short-circuit evaluation (Wikipedia)
The && operator is a short circuit operator in C++ and it will not evaluate bool2 if bool1 is false.
Short-circuit evaluation denotes the semantics of some Boolean operators in some programming languages in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression: for instance, when the first argument of the AND function evaluates to false, the overall value must be false; and when the first argument of the OR function evaluates to true, the overall value must be true.
In C++, both && and || operators use short-circuit evaluation.
What you're referring to is short circuit evaluation. I thought that it may be compiler specific, however that article I linked to shows it as language specific, and C++ does adhere. If it is indeed compiler specific, I can't imagine a compiler that wouldn't follow it. The day to day compiler I use at the moment, VS 2008, does. Basically it will follow the operator precedence, and as soon as the condition result is guaranteed,

C++ optimization of boolean or operator [duplicate]

I'm new to c++ and am curious how the compiler handles lazy evaluation of booleans. For example,
if(A == 1 || B == 2){...}
If A does equal 1, is the B==2 part ever evaluated?
No, the B==2 part is not evaluated. This is called short-circuit evaluation.
Edit: As Robert C. Cartaino rightly points out, if the logical operator is overloaded, short-circuit evaluation does not take place (that having been said, why someone would overload a logical operator is beyond me).
Unless the || operator is overloaded, the second expression will not be evaluated. This is called "short-circuit evaluation."
In the case of logical AND (&&) and logical OR (||), the second expression will not be evaluated if the first expression is sufficient to determine the value of the entire expression.
In the case you described above:
if(A == 1 || B == 2) {...}
...the second expression will not be evaluated because
TRUE || ANYTHING, always evaluates to TRUE.
Likewise,
FALSE && ANYTHING, always evaluates to FALSE, so that condition will also cause a short-circuit evaluation.
A couple of quick notes
Short circuit evaluation will not apply to overloaded && and || operators.
In C++, you are guaranteed that the first expression will be evaluated first. Some languages do not guarantee the order of evaluation and VB doesn't do short-circuit evaluation at all. That's important to know if you are porting code.
The B==2 part is not evaluated.
Be careful! Don't put something like ++B==2 over there!
C++ applies short circuiting to Boolean expression evaluation so, the B == 2 is never evaluated and the compiler may even omit it entirely.
The compiler handles this by generating intermediate jumps. For the following code:
if(A == 1 || B == 2){...}
compiled to pseudo-assembler, might be:
load variable A
compare to constant 1
if equal, jump to L1
load variable B
compare to constant 2
if not equal, jump to L2
L1:
... (complete body of if statement)
L2:
(code after if block goes here)
This is short-circuit evaluation, as James says. Lazy evaluation is something entirely different.
No it's not.
Same with &&, if one is wrong, it doesn't bother evaluating the other one.
B == 2 is never evaluated.
See Short-Circuit Evaluation for more information.

Is it guaranteed that unnecessary conditions are not evaluated? [duplicate]

This question already has answers here:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 5 years ago.
When encountering a (bool1 && bool2), does c++ ever attempts to check bool2 if bool1 was found false or does it ignore it the way PHP does?
Sorry if it is too basic of a question, but I really could not find a mentioning of that neither in Schildt nor on the Internet.
Yes, the && operator in C++ uses short-circuit evaluation so that if bool1 evaluates to false it doesn't bother evaluating bool2.
"Short-circuit evaluation" is the fancy term that you want to Google and look for in indexes.
The same happens with the || operator, if bool1 evaluates to true then the whole expression will evaluate to true, without evaluating bool2.
In case you want to evaluate all expressions anyway you can use the & and | operators.
C++ does use short-circuit logic, so if bool1 is false, it won't need to check bool2.
This is useful if bool2 is actually a function that returns bool, or to use a pointer:
if ( pointer && pointer->someMethod() )
without short-circuit logic, it would crash on dereferencing a NULL pointer, but with short-circuit logic, it works fine.
That is correct (short-cicuit behavior). But beware: short-circuiting stops if the operator invoked is not the built-in operator, but a user-defined operator&& (same with operator||).
Reference in this SO
The && operator short circuits in C++ - if bool1 was false in your example, bool2 wouldn't be checked/executed.
This is called short-circuit evaluation (Wikipedia)
The && operator is a short circuit operator in C++ and it will not evaluate bool2 if bool1 is false.
Short-circuit evaluation denotes the semantics of some Boolean operators in some programming languages in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression: for instance, when the first argument of the AND function evaluates to false, the overall value must be false; and when the first argument of the OR function evaluates to true, the overall value must be true.
In C++, both && and || operators use short-circuit evaluation.
What you're referring to is short circuit evaluation. I thought that it may be compiler specific, however that article I linked to shows it as language specific, and C++ does adhere. If it is indeed compiler specific, I can't imagine a compiler that wouldn't follow it. The day to day compiler I use at the moment, VS 2008, does. Basically it will follow the operator precedence, and as soon as the condition result is guaranteed,

How does C++ handle &&? (Short-circuit evaluation) [duplicate]

This question already has answers here:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 5 years ago.
When encountering a (bool1 && bool2), does c++ ever attempts to check bool2 if bool1 was found false or does it ignore it the way PHP does?
Sorry if it is too basic of a question, but I really could not find a mentioning of that neither in Schildt nor on the Internet.
Yes, the && operator in C++ uses short-circuit evaluation so that if bool1 evaluates to false it doesn't bother evaluating bool2.
"Short-circuit evaluation" is the fancy term that you want to Google and look for in indexes.
The same happens with the || operator, if bool1 evaluates to true then the whole expression will evaluate to true, without evaluating bool2.
In case you want to evaluate all expressions anyway you can use the & and | operators.
C++ does use short-circuit logic, so if bool1 is false, it won't need to check bool2.
This is useful if bool2 is actually a function that returns bool, or to use a pointer:
if ( pointer && pointer->someMethod() )
without short-circuit logic, it would crash on dereferencing a NULL pointer, but with short-circuit logic, it works fine.
That is correct (short-cicuit behavior). But beware: short-circuiting stops if the operator invoked is not the built-in operator, but a user-defined operator&& (same with operator||).
Reference in this SO
The && operator short circuits in C++ - if bool1 was false in your example, bool2 wouldn't be checked/executed.
This is called short-circuit evaluation (Wikipedia)
The && operator is a short circuit operator in C++ and it will not evaluate bool2 if bool1 is false.
Short-circuit evaluation denotes the semantics of some Boolean operators in some programming languages in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression: for instance, when the first argument of the AND function evaluates to false, the overall value must be false; and when the first argument of the OR function evaluates to true, the overall value must be true.
In C++, both && and || operators use short-circuit evaluation.
What you're referring to is short circuit evaluation. I thought that it may be compiler specific, however that article I linked to shows it as language specific, and C++ does adhere. If it is indeed compiler specific, I can't imagine a compiler that wouldn't follow it. The day to day compiler I use at the moment, VS 2008, does. Basically it will follow the operator precedence, and as soon as the condition result is guaranteed,

C++ short-circuiting of booleans

I'm new to c++ and am curious how the compiler handles lazy evaluation of booleans. For example,
if(A == 1 || B == 2){...}
If A does equal 1, is the B==2 part ever evaluated?
No, the B==2 part is not evaluated. This is called short-circuit evaluation.
Edit: As Robert C. Cartaino rightly points out, if the logical operator is overloaded, short-circuit evaluation does not take place (that having been said, why someone would overload a logical operator is beyond me).
Unless the || operator is overloaded, the second expression will not be evaluated. This is called "short-circuit evaluation."
In the case of logical AND (&&) and logical OR (||), the second expression will not be evaluated if the first expression is sufficient to determine the value of the entire expression.
In the case you described above:
if(A == 1 || B == 2) {...}
...the second expression will not be evaluated because
TRUE || ANYTHING, always evaluates to TRUE.
Likewise,
FALSE && ANYTHING, always evaluates to FALSE, so that condition will also cause a short-circuit evaluation.
A couple of quick notes
Short circuit evaluation will not apply to overloaded && and || operators.
In C++, you are guaranteed that the first expression will be evaluated first. Some languages do not guarantee the order of evaluation and VB doesn't do short-circuit evaluation at all. That's important to know if you are porting code.
The B==2 part is not evaluated.
Be careful! Don't put something like ++B==2 over there!
C++ applies short circuiting to Boolean expression evaluation so, the B == 2 is never evaluated and the compiler may even omit it entirely.
The compiler handles this by generating intermediate jumps. For the following code:
if(A == 1 || B == 2){...}
compiled to pseudo-assembler, might be:
load variable A
compare to constant 1
if equal, jump to L1
load variable B
compare to constant 2
if not equal, jump to L2
L1:
... (complete body of if statement)
L2:
(code after if block goes here)
This is short-circuit evaluation, as James says. Lazy evaluation is something entirely different.
No it's not.
Same with &&, if one is wrong, it doesn't bother evaluating the other one.
B == 2 is never evaluated.
See Short-Circuit Evaluation for more information.