Properties5

rw-book-cover


Booleans do not hold a monopoly of condition values: in Lua, any value can represent a con- dition. Conditional tests (e.g., conditions in control structures) consider both the Boolean false and nil as false and anything else as true. In particular, Lua considers both zero and the empty string as true in conditional tests.


Both and and or use short-circuit evaluation, that is, they evaluate their second operand only when nec- essary. Short-circuit evaluation ensures that expressions like (i ~= 0 and a/i > b) do not cause run-time errors: Lua will not try to evaluate a / i when i is zero.


we can select the maximum of two numbers x and y with the expression (x > y) and x or y. When x > y, the first expression of the and is true, so the and results in its second operand (x), which is always true (because it is a number), and then the or expression results in the value of its first operand, x.