The statements in Lua support assignment, control structures, function calls, and variable declarations.
Empty statement segments are not allowed, so;; is illegal.
1 Statement Group | Chuncks
Copy Code code as follows:
(['; '] should be behind the statement group; )
2 Statement blocks | Blocks
Copy Code code as follows:
Block:: = Chunck
Stat:: = do blocks End
A statement block can be written explicitly as a statement group and can be used to control the scope of a local variable.
3 Assignment | Assignment
Lua supports multiple assignments.
When multiple assignments are made, the value of the expression on the right is assigned to the left value in order. The right value is insufficient to fill nil, and the right value is superfluous.
Copy Code code as follows:
b = 1
A,b = 4--a = 4,b = Nil
+++
When Lua makes an assignment, it computes the right expression at once and assigns it.
Copy Code code as follows:
i = 5
I,a[i] = i+1, 7--i = 6, a[5] = 7
In particular, there
X,y = y,x-Exchange X,y value
+++
The meaning of assignment operations to global variables and to the fields of a table can be changed in the meta table.
4 Control structure
4.1-piece Statement
Copy Code code as follows:
IF [exp]
[Block]
ElseIf [Exp]
[Block]
Else
[Block]
End
4.2 Circular Statements
Copy Code code as follows:
While [exp]
[Block]
End
+++
Repeat
[Block]
Until [exp]
Note that since the repeat statement is not finished with until, local variables defined in the block can be used in an expression after until.
For example:
Copy Code code as follows:
A = 1
c = 5
Repeat
b = A + C
c = c * 2
Until B > 20
Print (c)--> 40
+++
4.3 Break and return
Break and return can only be written in the last sentence of the statement block, if you really need to write in the middle of the statement block, then surround the do-end statement block outside two keywords.
Do break end
5 for Loop
For the use of the loop more, alone to speak out.
The expression in the for is evaluated once before the start of the loop and is no longer updated during the loop.
5.1 Number Forms
for [Name] = [Exp],[exp],[exp] does [block] end
Three exp respectively represents initial value, ending values, stepping. The value of exp needs to be a number.
The third exp default is 1, which can be omitted.
Copy Code code as follows:
A = 0
For i = 1,6,2 do
A = a + I
End
Equivalent to
Copy Code code as follows:
int a = 0;
for (int i = 1; I <= 6;i + 2) {//equals sign, if step is negative, then I >= 6
A + = i;
}
5.2 Iterator Form
When an iterator outputs a table, if there are functions in the table, the order and number of outputs are uncertain (the results of the test are unknown).
The essence of A for loop in the form of an iterator
--Returns the iterator, the state table, the iterator initial value in turn
Copy Code code as follows:
function Mypairs (t)
function iterator (t,i)
i = i + 1
i = t[i] and I-if t[i] = = Nil I = nil; otherwise i = i
return I,t[i]
End
Return iterator,t,0
End
--a table
t = {[1]= "1", [2]= "2"}
--the equivalent form of an iterative form for statement
Copy Code code as follows:
Todo
Local F, s, var = mypairs (t)
While True
Local var1, var2 = f (S, Var)
var = var1
If var = nil then break end
--the statement added in the For loop
Print (VAR1,VAR2)
End
End
--Iteration form for statement
Copy Code code as follows:
For var1,var2 in Mypairs (t) do
Print (VAR1,VAR2)
End
--> 1 1
--> 2 2
--> 1 1
--> 2 2
5.2.1 Array Form
Copy Code code as follows:
ary = {[1]=1,[2]=2,[5]=5}
For i,v in Ipairs (ary) do
Print (v)--> 1 2
End
Starts at 1 and ends when the numeric subscript ends or the value is nil.
5.2.2 Table Traversal
Copy Code code as follows:
Table = {[1]=1,[2]=2,[5]=5}
For k,v in pairs (table) do
Print (v)--> 1 2 5
End
Iterate through the key-value pairs of the entire table.
For more information about iterators, refer to the LUA iterator and generics for.
The above mentioned is the entire content of this article, I hope you can enjoy.