LUA allows "multiple assignments," which means that multiple values are assigned to multiple variables at once. Each value or variable is separated by commas:
A, B = ten, 2*x
In multiple assignments, LUA evaluates all the elements on the right side of the equal sign before performing the assignment.
X, y = y, X--and swap x with Y
Lua always adjusts the number of values on the right side of the equals sign to match the number of variables on the left. The rule is: if the number of values is less than the number of variables, then the extra variables will be assigned nil, if the number of values more, then the extra value will be "silently" discarded.
Multiple assignments are generally seldom used, but they are useful when exchanging two variables and collecting multiple return values for a function. For example, A, B = f (), function f will return two values, a receives the first, and B receives the second one.
"Using local variables as much as possible" is a good programming style. Local variables can avoid introducing some useless names into the global environment, avoiding messing up the global environment. In addition, accessing local variables is faster than accessing global variables. Finally, a local variable usually disappears with the end of its scope, which allows the garbage collector to release its value.
i = ten--and global variables Local j = 2--and local variables
In Lua, there is a customary notation:
local foo = foo
This code creates a local variable, foo, and initializes it with the value of all variable foo. If subsequent functions change the value of the global foo, you can save its value here first. This approach also accelerates access to Foo at the current scope.
LUA does not support switch statements, so a series of If then ElseIf else end codes are common:
If op = = "+" then r = A + belseif op = = "-" Then r = A-belseif OP = = "*" r = A * Belseif op = = "/" R = A/b Else error ("Invalid operation") end
For While,lua to test its condition first, if the condition is false, then the loop ends, or LUA executes the loop body and repeats the process:
Local i = 1while A[i] do print (a[i]) i = i + 1end
The Repeat-until statement repeatedly executes its loop body until the condition is true. The test is done after the body of the loop, so the loop body executes at least once.
--Print the first line of the input is not empty content repeat lines = Io.read () until-~= "" Print
Unlike most other languages, the scope of a local variable declared in the loop body includes a conditional test in Lua:
Local SQR = X/2repeat Sqr = (Sqr + x/sqr)/2 Local error = Math.Abs (sqr^2-x) until error < x/10000- Here you can still access the error
The For statement has two forms: a numeric type for and a generic for.
The syntax for the digital for IS as follows:
For Var=exp1, EXP2, exp3 do < actuator >end
var changes from Exp1 to EXP2, each change takes exp3 as the step increment Var, and executes the "executor" once. The third expression, EXP3, is optional and, if not specified, LUA defaults to 1.
It is important to note that the control variable is automatically declared as a local variable for the For statement and is visible only within the loop body. Therefore, the control variable does not exist after the loop has ended. If you need to access the value of the control variable at the end of the loop (usually in the break loop), you must save the value to another variable!
A generic for loop iterates through all values through an iterator (iterator) function:
--Print all values of array A for I, V in Ipairs (a) do print (v) end
Lua's base library provides ipairs, an iterative function for iterating through an array. In each loop, I is given an index value, and V is given an array element value corresponding to that index.
--Print all keyfor K in pairs (t) does print (k) End in table t
Lua Chapter III-Syntax