-- 別擔心,double的64位中有52位用于
-- 保存精確的int值; 對于需要52位以內的int值,
-- 機器的精度不是問題。
t = "雙引號也可以"
u = [[ 兩個方括號
用于
多行的字符串。]]
t = nil -- 未定義的t; Lua 支持垃圾收集。
while num < 50 do
num = num + 1 -- 沒有 ++ or += 運算符。
end
if num > 40 then
print('over 40')
elseif s ~= 'walternate' then -- ~= 表示不等于。
-- 像Python一樣,== 表示等于;適用于字符串。
io.write('not over 40\n') -- 默認輸出到stdout。
else
-- 默認變量都是全局的。
local line = io.read() -- 讀取stdin的下一行。
print('Winter is coming, ' .. line)
end
-- 這不會出錯:
foo = anUnknownVariable -- 現在 foo = nil.
aBoolValue = false
--只有nil和false是fals; 0和 ''都是true!
if not aBoolValue then print('twas false') end
-- 類似于C/js里的 a?b:c 操作符:
ans = aBoolValue and 'yes' or 'no' --> 'no'
for i = 1, 100 do -- 范圍包括兩端
karlSum = karlSum + i
end
fredSum = 0
for j = 100, 1, -1 do fredSum = fredSum + j end
通常,范圍表達式為begin, end[, step].
repeat
print('the way of the future')
num = num - 1
until num == 0