and break do else elseif end false for function if in local nil not or repeat return then true until while
-- This is a single line comment --[[ This is a block of comment text ]]
+ addition 1 + 2 => 3 - substraction 2 - 1 => 1 * multiplication 3 * 4 => 12 / division 9 / 3 => 3 % modulo 8 % 6 => 2 ^ exponent 2 ^ 5 => 32
== equality ~= inequality < less-than <= less-than or equal > greater-than >= greater-than or equal
and (returns first argument if false/nil, otherwise returns second argument) or (returns the first argument if not false/nil, otherwise returns the second argument) not (returns true or false, representing the opposite of the supplied argument) 10 or 20 --> 10 10 and 20 --> 20 nil or 10 --> 10 nil and 10 --> nil
a = 1 a, b = 1, 2 Multiple assignment at the same time a, b = b, a Variable Swap _, _, a = 1, 2, 3 Common usage of "throw away" variables
aString = 'single quotes work' bString = "double quotes work" cString = [[hell, even double square brackets work]] dNumber = 12345.6789
name = "Jason" print("Hello, "..name..", it is very good to meet you!") results: Hello, Jason, it is very good to meet you!
name = "Jason" print(#name) results: 5
if i == 1 then print("One") elseif i==2 then print("Two") else print(i) end
for i = 1, 5, 1 -- start, limit, increment do print(i.."\n") end results: 1 2 3 4 5
i = 3 while i > 1 do print(i.."\n") i = i - 1 end repeat print(i.."\n") i = i + 1 until i = 3 results: 3 2 <-- last output of the WHILE 1 <-- first output of the REPEAT 2
function add (input1, input2) return input1 + input2 end (equivalent to) add = function(input1, input2) return input1 + input2 end function MultiValue () return 1, 2, 3, 4 end i = add(1, 2) print(i) result => 3 _, _, a, b = MultiValue() print(a..","..b) result => 3,4
a = {101, 202, 303, 404, 505} print(a[2]) result => 202
a = {[3] = 101, [4] = 202, 303, 404, fifth = 505} print(a[2]) result => 404 print(a[3]) result => 101 print(a.fifth) result => 505
name = "Fred" a = {[name] = "Flintstone", name = "Barney"} print(a[name]) result => Flintstone print(a.name) result => Barney print(a.Fred) result => Flintstone
a = { } a.Name = "Jason" is equivalent to: a = { ["Name"] = "Jason" } but not: a = { ["name"] = "Jason" }