I stumbled upon the following construction in C++:
bool result = false;
for(int i = 0; i<n; i++){
result |= TryAndDoSomething(i);
}
I supposed that this |=
was a shortcut for the OR operator, and that result
would equal true
in the end if at least one of these calls to TryAndDoSomething
had returned true
.
But now I am wondering if more than one call can actually return true
. Indeed if we extend the operation as:
result = result || TryAndDoSomething(i);
Then the method will be called only if return evaluated to false
, that is, if no other call before returned true
. Thus after one call returning true
, no other call will be done.
Is this the correct interpretation?
Best Answer
It's bitwise OR assignment, not short-circuited OR evaluation.
It is equivalent to:
Not: