Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Two dashes start a one-line comment.
- num = 42 -- All numbers are doubles.
- s = "walternate" -- Immutable strings like Python.
- t = nil -- Undefines t;
- -- Blocks are denoted with keywords like do/end:
- while num < 50 do
- num = num + 1 -- No ++ or += type operators.
- end
- -- An infinite loop that doesn’t end until Ctrl-R or Ctrl-T
- while true do
- --do stuff over and over
- end
- -- If statement:
- if num > 40 then
- print("over 40")
- elseif s ~= "walternate" then -- ~= is not equals.
- -- Equality check is == like Python; ok for strings.
- write("not over 40\n") -- Another way to write out to print.
- else
- -- Variables are global by default.
- thisIsGlobal = 5
- -- How to make a variable local:
- local line = read() -- Reads next input line.
- -- String concatenation uses the .. operator:
- print("Winter is coming, "..line)
- -- Replace/Hide the input characters the user types with *
- local password = read("*")
- end
- -- An undefined variable return “nil”
- -- This is not an error:
- foo = anUnknownVariable -- Now foo = nil.
- karlSum = 0
- for i = 1, 100 do -- The range includes both ends (“1” and “100”)
- karlSum = karlSum + i
- end
- -- Use "100, 1, -1" as the range to count down:
- fredSum = 0
- for j = 100, 1, -1 do
- fredSum = fredSum + j
- end
- function fib(n)
- if n < 2 then return 1 end
- return fib(n - 2) + fib(n - 1)
- end
Advertisement
Add Comment
Please, Sign In to add comment