Tutorial by Examples

Booleans and other values When dealing with lua it is important to differentiate between the boolean values true and false and values that evaluate to true or false. There are only two values in lua that evaluate to false: nil and false, while everything else, including the numerical 0 evaluate to...
Conditional contexts in Lua (if, elseif, while, until) do not require a boolean. Like many languages, any Lua value can appear in a condition. The rules for evaluation are simple: false and nil count as false. Everything else counts as true. if 1 then print("Numbers work.") ...
In Lua, booleans can be manipulated through logical operators. These operators include not, and, and or. In simple expressions, the results are fairly straightforward: print(not true) --> false print(not false) --> true print(true or false) --> true print(false and true) --> false ...
Logical operators in Lua don't "return" boolean, but one of their arguments. Using nil for false and numbers for true, here's how they behave. print(nil and nil) -- nil print(nil and 2) -- nil print(1 and nil) -- nil print(1 and 2) -- 2 print(nil or n...
In lua, the logical operators and and or returns one of the operands as the result instead of a boolean result. As a consequence, this mechanism can be exploited to emulate the behavior of the ternary operator despite lua not having a 'real' ternary operator in the language. Syntax condition and...

Page 1 of 1