Logical operators are also evaluated from the left to the right
View options
- Truncate descriptions
In 1-ProceduralProgramming/2-Conditions-and-loops.ipynb section Logical operators, it is explained that:
- and takes precedence over or
- Builtin operators && and || perform short-circuit evaluation: they do not evaluate the second operand if the result is known after evaluating the first
but we do not explained that they are evaluated from the left to the right.
Moreover, this example is a bit difficult:
if (b++ == 3 && a < 0)
;
std::cout << "b was incremented: " << b <<
" (as first operation is true second one is evaluated)." << std::endl;
FMHO, it would be easier to understand if it was the contrary than the example that is above:
if (a > 0 && b++ == 3)
;
std::cout << "b was incremented: " << b <<
" (as first operation is true second one is evaluated)." << std::endl;
- Show labels
- Show closed items