Introduction to Lua Programming
Written By: S Kokila
- 31 Aug 2007 -
Description: Lua is a powerful, light-weight programming language designed for extending applications. Coupled with it being relatively fast and having a very lenient license, it has gained a following among game developers for providing a viable scripting interface. It has been used in games such as World of Warcraft and Far Cry, and in applications such as Adobe Photoshop Lightroom and Snort.
- What is Lua?
- Getting Started
- Identifiers, Types, and Values
- Variables and Expressions
- Operators
- Statements and Assignments
- Control Structures
- File I/O
Arithmetic Operators
Lua provides the usual operators: the binary '+' (addition), '-' (subtraction), '*' (multiplication), '/' (division), and '^' exponentiation; and unary '-' (negation).
The operand can be numbers or strings that can be converted to numbers. All the operators except exponentiation have the usual meaning. The exponentiation operator calls the math.pow function in the standard mathematical library.
-- Arithmetic Operators a=5 b=3 print(a+b) -- 8 print(a-b) -- 2 print(a*b) -- 15 print(a/b) -- 1.667 print(a^b) -- 125 calls the math.pow function print(-a) -- -5
Relational Operators
The relational operators allow two values to be compared and always result in true or false values. Lua provides the following operators: '==' (equality), '~=' (inequality), '<' (less than), '>' (greater than), '<=' (less than or equal to) and '>=' (greater than or equal to).
The == and ~= operators first compares the types of the operands. It the types are different, the result of the expression is false. If the types are same, then the values of the operands are compared for numbers and strings in the usual way. However, the automatic conversion of string and number values do not apply to equality comparison, as 0 and "0" are two different types. Objects (tables, userdata, threads, and functions) are compared by reference. Two objects are equal only if they are the same object.
-- Equality Operators a=7; b=5; c="a string" print(a==c) -- false, as a holds a number and c a string print(a~=c) -- false here too for the same reason print(a==b) -- false as 7 is not equal to 5 print(a~=5) -- true as 7 is not equal to 5
The <, >, <=, >= operators work in the usual way.
Logical Operators
The logical operators in Lua are and, or and not. All logical operators consider nil and false as false, and anything else as true (in particular, the number 0 and empty string are also true).
The operator not always returns false or true.
The and operator returns its first argument if this value is false or nil, otherwise it returns its second argument. The or operator returns its first argument if this value is true, otherwise it returns its second argument. Both the and and or operators use short-cut evaluation, that is, the second argument is evaluated only if necessary. For example, consider the code below:
a=5;b=3 print(nil and (c=a+b)) -- nil, as the first argument is nil print(c) -- nil, as the second argument is -- evaluated by and operator
The expression c=a+b is not evaluated as the first argument is nil. Few more expressions here would help you understand the logical operators:
-- Logical Operators print(0 and false) -- false, as 0 yields true print(false and 5) -- false print(4 and nil) -- 4 print(4 or 7) -- 7 print(nil or 3) -- 3 print(false or nil) -- nil print(false and nil) -- false print(not false) -- true print(not nil) -- true print(not 56) -- false
Concatenation
Lua provides '..' as the concatenation operator to concatenate two strings. If the operands are numbers, they are automatically converted to strings.
-- Concatenation print("Learning " .. "Lua!") -- Learning Lua!
Operator Precedence and Associativity
Precedence controls the order of evaluation of operators in an expression and associativity controls the order of evaluation of operators with same precedence level. The precedence of operators is given below, from lower to higher priority: or
and
< > <= >= ~= ==
..
+ -
* /
not – (unary)
^
The concatenation (..) and exponentiation (^) operators are right associative and rest of the operators are left associative.