Three views of the ruined variable definitions

Variable definitions Variable Definition tells the interpreter to create a storage location and number of variables. With optional type of variable definitions, and the type comprising a list of one or more variables, as follows -
type variable_list;

Here, type is an alternative type of local or global, and may contain one or more variable_list identifier names separated by commas. Here shows some valid statements -
local I, J
local I
local A, C

Local row i, j are declared and defined variables i and J; it instructs the interpreter to create a variable named i, j, and is limited to a partial range.
Variables can be initialized (initial value assigned) in a statement. Initialization procedure contains an equal sign, followed by a constant expression, as follows -
type variable_list = VALUE_LIST;

同样的一些示例如下 -
local d , f = 5 ,10 --declaration of d and f as local variables.
d , f = 5, 10; --declaration of d and f as global variables.
d, f = 10 --[[declaration of d and f as global variables.
Here value of f is nil --]]

The definition does not initialize program: variables with static storage duration initialized using implicit nil.
Lua variable declarations, as in the above example can be seen, the assignment of multiple variables follow variable_list and value_list format. In the example above, local d, f = 5, 10 in the variable_list
is d and f, is in the 5 and 10 value_list.
Lua, assignment variable_list value of the first variable, value_list a value similar to the first, and so on. Thus, d value of 5, f is 10.
Example
Consider the following example in which the variables are declared at the top, but they have the main function definition and initialization -
Variable Definition -:
local A, B

-- Initialization
a = 10
b = 30

print("value of a:", a)

print("value of b:", b)

-- Swapping of variables
b, a = a, b

print("value of a:", a)

print("value of b:", b)

f = 70.0/3.0
print("value of f", f)

Build and execute the above code, the following consequences -
value of A: 10
value of B: 30
value of A: 30
value of B: 10
value of F 23.333333333333

Left and right values ​​in Lua there are two expressions -

Left value - expression that references a memory location called "Left value" expression. Left can be displayed as left or right side of an assignment. Rvalue - the right term value is an address value of the data stored in memory. The right value is not
capable of assignment expression, it may appear to the right of the right value, but will not appear in the left side of an assignment.
Variable value is left, it may appear to the left of the assignment. Digital text is the right value, and therefore may not be assigned, nor appears on the left. The following is a valid statement -
G = 20

But the following is not a valid statement, it produces an error when building -
10 = 20

In Lua programming language, in addition to the above-described type assignment, the same statement can have a plurality of left and right values. As follows.
g, l = 20,30

In the above statement, 20 assigned to the variable g, 30 assigned to the variable l.

 

Guess you like

Origin www.cnblogs.com/gd-luojialin/p/10962722.html