The basic syntax
Lua as C and PASCAL supports almost all of the traditional statements: assignment, control structures statement, function calls, etc., but also supports non-traditional multi-variable assignment, local variable declarations.
Assignment
Assignment is to change the value of a variable and changing the basic method table fields.
a = "hello" .. "world"
t.n = t.n + 1
Lua simultaneous assignment to multiple variables, each element of the list of variables and list of values separated by commas, assign values to the right of the statement sequentially assigned to the variable on the left.
a, b = 10, 2*x <--> a=10; b=2*x
Lua will first encounter assignment statement to calculate all values to the right and then perform the assignment, so we can carry out the exchange value of the variable:
x, y = y, x -- swap 'x' for 'y'
a[i], a[j] = a[j], a[i] -- swap 'a[i]' for 'a[i]'
When the number of variables and the number of inconsistent values, Lua would have been to the number of variables is based on the following strategies:
a. 变量个数>值的个数 按变量个数补足 nil
b. 变量个数<值的个数 多余的值会被忽略
E.g:
a, b, c = 0, 1
print(a,b,c) --> 0 1 nil
a, b = a+1, b+1, b+2 -- value of b+2 is ignored
print(a,b) --> 1 2
a, b, c = 0
print(a,b,c) --> 0 nil nil
The last example above is a common error conditions Note: If you want to assign a number of variables must be assigned to each variable in turn.
a, b, c = 0, 0, 0
print(a,b,c) --> 0 0 0
Multi-valued assignment is often used to exchange variable, or a function call to return to the variable:
a, b = f()
f () returns two values, a first assigned a, the second is assigned to b.
Local variables of code block (block)
Use local creates a local variable, global variables, local variables are only valid within that block is declared. Block: a control means within the structure, a function thereof, or a the chunk (file or text string variable that is declared).
x = 10
local i = 1 -- local to the chunk
while i<=x do
local x = i*2 -- local to the while body
print(x) --> 2, 4, 6, 8, ...
i = i + 1
end
if i > 20 then
local x -- local to the "then" body
x = 20
print(x + 2)
else
print(x) --> 10 (the global one)
end
print(x) --> 10 (the global one)
Note that, in the above example, if the interactive mode may not output the desired result, because the second sentence is a local i = 1 full chunk, executes this post in an interactive mode, Lua will start a new chunk, so i second sentence beyond his range. This code can be placed do ... end (corresponding to c / c ++ {a}) block.
It should be possible to use local variables, there are two advantages:
- Avoid naming conflicts
- Speed access local variables faster than global variables.
We gave a clear delineation block boundaries: do ... part in the end. When you want better control the scope of local variables when it is useful.
do
local a2 = 2*a
local d = sqrt(b^2 - 4*a*c)
x1 = (-b + d)/a2
x2 = (-b - d)/a2
end -- scope of 'a2' and 'd' ends here
print(x1, x2)
Control structure statement
The conditional expression result control structure can be any value, Lua considers to be false and nil as false the other is true. if statement, there are three forms:
if conditions then
then-part
end;
if conditions then
then-part
else
else-part
end;
if conditions then
then-part
elseif conditions then
elseif-part
.. --->多个 elseif
else
else-part
end;
while statement:
while condition do
statements;
end;
repeat-until statement:
repeat
statements;
until conditions;
for there are two statements:
first, the value for loop:
for var=exp1,exp2,exp3 do
loop-part
end
as it will be used for exp3 EXP1 from step (initial value) to EXP2 (end value), performs loop-part. Wherein exp3 can be omitted, step = 1 There are several points to note:
- Three expressions will only be counted once, and before the cycle begins.
for i=1,f(x) do
print(i)
end
for i=10,1,-1 do
print(i)
end
The first example f (x) will be called once before the loop.
2. var control variable is automatically declared local variable, and are valid only in the circulation.
for i=1,10 do
print(i)
end
max = i -- probably wrong! 'i' here is global
If the value of the control variable of the need to retain, in the need to save cycle
-- find a value in a list
local found = nil
for i=1,a.n do
if a[i] == value then
found = i -- save value of 'i'
break
end
end
print(found)
- Do not change the value of the recycling process control variables, the results of doing that is unpredictable. If you want to exit the loop, break statement.
Second, the paradigm for loop:
already seen an example:
-- print all values of array 'a'
for i,v in ipairs(a) do print(v) end
Paradigm for traversing each iteration subroutine return a value.
Look a key example of the traverse table:
-- print all keys of table 't'
for k in pairs(t) do print(k) end
And paradigm for the same values for points:
4. control variables are local
5. not modify the values of the control variable
look at an example, consider a table:
days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
Now wants to convert the name of the corresponding day of the week, an effective way to solve the problem is to construct a reverse table:
revDays = {["Sunday"] = 1, ["Monday"] = 2,
["Tuesday"] = 3, ["Wednesday"] = 4,
["Thursday"] = 5, ["Friday"] = 6,
["Saturday"] = 7}
Here you can easily get answers to the questions:
x = "Tuesday"
print(revDays[x]) --> 3
We do not need the manual, it can automatically construct the reverse table
revDays = {}
for i,v in ipairs(days) do
revDays[v] = i
end
If you are still somewhat unclear paradigm for the later chapters we will continue to learn.
break and return statement
break statement to exit the current loop (for, repeat, while). Not be used in an external circulation.
return from the function to return a result, when a function will have a natural end end return default. (This function is similar to the process pascal)
Lua syntax requires break and return only appear at the end of a block (that is to say: as the last one chunk, or before the end, or else before, or before until), for example:
local i = 1
while a[i] do
if a[i] == v then break end
i = i + 1
end
有时候为了调试或者其他目的需要在 block 的中间使用 return 或者 break,可以显式
的使用 do..end 来实现:
function foo ()
return --<< SYNTAX ERROR
-- 'return' is the last statement in the next block
do return end -- OK
... -- statements not reached
end