if( condition && "Text in double quotes")
{
do this;
}
else
{
do that;
}
Saw this code in a program, in which case would the conditional expression in the IF-statement return true and in which case would it return false?
if( condition && "Text in double quotes")
{
do this;
}
else
{
do that;
}
Saw this code in a program, in which case would the conditional expression in the IF-statement return true and in which case would it return false?
false && "Literally anything"
is always false
.
The primary expression is false
, operator &&
is a logical operator (unless it was overloaded), so it will short-circuit.
This is because false && E
(where E is an expression) is known to always evaluate to false
. Therefore E
doesn't need to be evaluated at all.
So if E
was a function call with side effects: the side effects won't occur since the function is never called.
Example:
int main() { return false && "anything"; } // returns 0
Live on compiler explorer