Hello RonW,
I’m sorry, but you’re wrong about C:
C also doesn't guarantee order of evaluation
...
Proper evaluation of or doesn't require a particular order for evaluating its operands to get the correct result. This is because or is transitive.
To take the last part first: logical or can’t be transitive, because transitivity applies only to those operators which are also relations, and logical or is not a relation. But suppose it were; and let propositions p, q, r have values false, true, false, respectively. Then p OR q is true, because false OR true is true; and q OR r is also true, because true OR false is true; so, by transitivity, p OR r should also be true. But it isn’t, because false OR false is false.
Now to the important issue. The C standard does guarantee the order of evaluation for logical OR (||):
Unlike the bitwise | operator, the || operator guarantees left-to-right evaluation; if the second operand is evaluated, there is a sequence point between the evaluations of the first and second operands. If the first operand compares unequal to 0, the second operand is not evaluated.1
To the best of my knowledge, this has always been an important feature of C. This is true for logical AND (&&) as well as logical OR. For example, when dealing with pointers it is often necessary to test whether the pointer is null before attempting a dereference:
int *p; ... if (p && *p > 10) ...
If the order of evaluation of logical AND (&&) were not guaranteed by the C standard (as it is2), the *p operation could be performed on a null pointer before the pointer is checked, causing the program to segfault. This possibility is explicitly excluded by the standard.
1The C11 Standard, document WG14 N1570 (working paper, 2011-04-12), downloaded from here. The behaviour of the logical OR operator is specified in section 6.5.14, paragraph 4.
2Ibid, section 6.5.13, paragraph 4.
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re^9: next unless condition
by Athanasius
in thread next unless condition
by hankcoder
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |