Game Development Reference
In-Depth Information
Table 1-1. Arithmetic Operators
Operator
Description
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Modulo
^
Exponent
-
Unary negation
Relational Operators
These are the operators used for comparing or conditions. They're listed in Table 1-2 .
Table 1-2. Relational Operators
Operator
Description
==
Equality (to check for equality between two values)
~=
Not equal to (the opposite of equality)
<
Less than
>
Greater than
<=
Less than or equal to
>=
Greater than or equal to
These operators will always result in a true or a false return value. It should be noted that when
comparing two numbers or values, use the == where as a single = in Lua signifies assignment.
Logical Operators
The logical operators in Lua are
and
or
not
The way the and and or work in Lua is by what's called the shortcut evaluation . It checks for a value,
and checks further only if it is required. and returns the first arguments if the value is false or nil ; if it
isn't, then it returns the second argument. or , on the other hand, returns the first value if the result is
not false or nil , and returns the second argument if the first argument is false or nil .
The best use of or is in functions for assigning default values:
a = a or 5 -- Can be used to assign the value of 5 if the value of a is nil or false
 
Search WWH ::




Custom Search