Skip to content Skip to sidebar Skip to footer

Short Circuit Evaluation Order

All this time my thinking of short circuit evaluations seems to be wrong. In javascript: var a = false, b = true, c=true; a && b || c; // Evaluates to true Compared to var

Solution 1:

These simple rules apply:
- shortcuts in logical expression evaluation does not mean that expressions are evaluated incorrectly, i.e. the result is the same with or witout shortcutting;
- the AND boolean operator (&&) is of higher precedence than the OR (||). This is why a && b are 'wrapped' together;
- it is not safe to rely on precedence order, use parentheses ; this improves readability too;
- most languages do shortcuts when evaluating logical expressions if the result is already defined, incl. PHP; There are exceptions however most notably in reverse polish notation languages like PostScript.


Post a Comment for "Short Circuit Evaluation Order"