Arithmetic operators
Lua's arithmetic operators are:
"+" (addition):
The code copy is as follows:
print(1 + 2)
"-" (subtraction):
The code copy is as follows:
print(2 - 1)
"*"(multiplication):
The code copy is as follows:
print(1 * 2)
"/"(division):
The code copy is as follows:
print(1 / 2)
"^" (index):
The code copy is as follows:
print(27^(-1/3))
"%" (model):
The code copy is as follows:
print(5 % 3)
Relational operators
Lua provides the following relational operators:
The code copy is as follows:
< > <= >= == ~=
The operations returned by the above operators are true or false. Strings cannot be compared with numbers
Logical operators
Logical operators include and, or, not
The code copy is as follows:
print(1 and 2)
print(nil and 1)
print(false and 2)
print(1 or 2)
print(false or 5)
Logical operators treat false and nil as false, and others as true.
Local variables and scope
Lua creates local variables through local statements, and the scope of local variables is limited to the block that declares them.
The code copy is as follows:
local a, b = 1, 10
if a < b then
print(a)
local a
print(a)
end
print(a, b)
Saving global variables with local variable local can speed up access to global variables in the current scope. For the acceleration effect, compare the execution time of Fibonacci sequence below to calculate the following:
The code copy is as follows:
function fibonacci(n)
if n < 2 then
Return n
end
return fibonacci(n - 2) + fibonacci(n - 1)
end
io.write(fibonacci(50), "/n")
Use local variable local
The code copy is as follows:
local function fibonacci(n)
if n < 2 then
Return n
end
return fibonacci(n - 2) + fibonacci(n - 1)
end
io.write(fibonacci(50), "/n")
Control structure
if then elseif else end
The code copy is as follows:
if num == 1 then
print(1)
elseif num == 2 then
print(2)
else
print("other")
end
Lua does not support switch statements
While
First judge the while condition. If the condition is true, then execute the loop body, otherwise it will end
The code copy is as follows:
local i = 1
while a[i] do
print(a[i])
i = i + 1
end
repeat-until
First execute the circulation body once, then judge the condition. If the condition is true, exit the circulation body, otherwise continue to execute the circulation body. Similar to do-while statements in other languages, the loop body will be executed at least once.
The code copy is as follows:
local a = 1
repeat
a = a + 1
b = a
print(b)
until b < 10
for loop
There are two forms for loop statements: numeric for (numeric for), generic for (generic for)
Number for syntax:
The code copy is as follows:
for start, end, step do
doing something
end
start is the start value, end is the end value, and step is the step size (optional, default is 1)
The code copy is as follows:
for a = 10, 0, -2 do
print(a)
end
A generic for loop traverses all values through an iterator function:
The code copy is as follows:
tab = { key1 = "val1", key2 = "val2", "val3" }
for k, v in pairs(tab) do
if k == "key2" then
break
end
print(k .. " - " .. v)
end
The break and return statements are used to break out of the currently executed block.