-- Different types --
local x = 10 --number
local name = "john doe" --string
local isAlive = false -- boolean
local a = nil --no value or invalid value
print(type(name))
print(type(x))
print(type(5.8))
print(type(isAlive))
print(type(A==B))
print(type(print))
print(type(nil))
print(type("string"))
print(type({}))
--[[ 隱藏文字/註解
]]
-- Operators --
-- + addition
-- - minus
-- * multiply
-- / divide
-- ^ power
-- % modulus
-- # number
-- and : Logical AND
-- or : Logical OR
print("----Numbers Operators----")
local a = 1
local b = 2
local c = a + b
print(c) -- 3
print(a + b)
print(-b) -- -2
local x = 1 * 3 * 4 -- 12
print(x)
print(1 * 3 * 4)
print((1+3) * 2)
print(10/2) -- 5
print (2^2) -- 4
print(5%2) -- 1
print("----Concatenation----")
print("It" .. " is " .. a .. ".")
print("----increment----")
local level = 1
level = level + 1
print(level) -- 2
print("----Boolean----")
VARA = 123
VARB = 456
VARC = VARA + VARB
print(VARA == VARB)
local isAlive = true
print(isAlive)
-- Conditional Statements --
-- Comparison Operators --
-- == equality
-- ~= inequality
-- < less than
-- > greater than
-- <= less than or equal to
-- >= greater than or equal to
print("----IF----")
local age = 10
if age > 18 then
print("over 18")
end
local isAlive = true
if isAlive then
print("dog")
end
print("----elseif and else----")
age = 20
if age > 18 then
print("Adult!")
elseif age == 18 then
print("Congratulate!")
else
print("No drink!")
end
print("----For----")
local a = 0
for i = 1,10 do
a = a+i
end
print(a)
print("----Combining Statements----")
local x = 10
if x == 10 and x < 0 then --> both are true
print("dog")
elseif x == 10 or x < 0 then --> 1 or more are true
print("cat")
end
print("----Nested statements----")
local x = 10
local isAlive = false
if x==10 then
if isAlive == true then
print("True")
else
print("False")
end
end
print("----Invert Value----")
local x = 100
if not (x == 10 ) then
print("x ~= 10")
end
print("----Function----")
function test(s, e)
local a = 0
for i = s, e do
a = a+i
end
print(a)
end
test(1,10)
To embed this program on your website, copy the following code and paste it into your website's HTML: