SoFunction
Updated on 2025-04-13

A brief discussion on Lua sentences

Statements in Lua support assignment, control structure, function calls, and variable declarations.

Empty statement segments are not allowed, so ;; is illegal.

1 statement group | chunks

Copy the codeThe code is as follows:

chunck ::= {stat[';']}

([';'] should mean that the statement group is followed by ; is optional.)

2 statement blocks | blocks

Copy the codeThe code is as follows:

block ::= chunck
stat ::= do block end

A statement block can be explicitly written into a statement group, which can be used to control the scope of action of local variables.

3 Assignment | assignment

Lua supports multiple assignments.

When multiple assignments are made, the value of the expression on the right is assigned to the lvalue in order. If the rvalue is insufficient, nil will be added, and the rvalue will be redundant.

Copy the codeThe code is as follows:

b = 1
a,b = 4 -- a = 4,b = nil
+++

When Lua performs assignment operations, it will calculate all the expressions on the right at one time and then assign values.

Copy the codeThe code is as follows:

i = 5
i,a[i] = i+1, 7 -- i = 6 ,a[5] = 7

In particular, there is

x,y = y,x -- swap the values ​​of x,y
+++

The meaning of assignment operations to global variables and fields of tables can be changed in the meta table.

4 Control structure

4.1 Conditional Statement

Copy the codeThe code is as follows:

if [exp]
    [block]
elseif [exp]
    [block]
else
    [block]
end

4.2 Loop statements

Copy the codeThe code is as follows:

while [exp]
    [block]
end
+++

repeat
    [block]
until [exp]

Note that since the repeat statement has not ended until, local variables defined in block can be used in expressions after until.

For example:

Copy the codeThe code is 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 it really needs to be written in the middle of the statement block, then the do end statement block is surrounded outside the two keywords.

do break end

5 For loop

There are many ways to use for loops, so I will talk about it separately.

Expressions in for will be evaluated at once before the loop starts and will not be updated during the loop.

5.1 Digital Form

for [Name] = [exp],[exp],[exp] do [block] end
The three exps represent the initial value, the end value, and the step. The values ​​of exp need to be a number.
The third exp defaults to 1 and can be omitted.

Copy the codeThe code is as follows:

a = 0
for i = 1,6,2 do
    a = a + i
end

Equivalent to

Copy the codeThe code is as follows:

int a = 0;
for (int i = 1; i <= 6;i += 2){ // Get the equal sign, if the step is negative, then i >= 6 will be taken
    a += i;
}

5.2 Iterator form

When outputting a table in an iterator form, if there are functions in the table, the order and number of outputs are uncertain (the results obtained by the author's test are unknown for the specific reason).

The essence of a for loop in the form of an iterator

-- Return to the iterator, status table, and iterator initial values ​​in turn

Copy the codeThe code is as follows:

function mypairs(t)
    function iterator(t,i)
        i = i + 1
i = t[i] and i      -- If t[i] == nil then i = nil; otherwise i = i
        return i,t[i]
    end
    return iterator,t,0
end

-- A table
t = {[1]="1",[2]="2"}

-- Iterative form for statement equivalent form

Copy the codeThe code is as follows:

do
local f, s, var = mypairs(t)
    while true do
        local var1, var2 = f(s, var)
        var = var1
        if var == nil then break end

-- statement added in the for loop
        print(var1,var2)

    end
end

-- Iterative form for statement

Copy the codeThe code is 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 the codeThe code is as follows:

ary = {[1]=1,[2]=2,[5]=5}
for i,v in ipairs(ary) do
    print(v)                    --> 1 2
end

Starting from 1, until the end of the numerical subscript or the value is nil.

5.2.2 Table traversal

Copy the codeThe code is as follows:

table = {[1]=1,[2]=2,[5]=5}
for k,v in pairs(table) do
    print(v)                    --> 1 2 5
end

Iterates over the key-value pairs of the entire table.

For more information about iterators, please refer to Lua iterators and generics for.

The above is the entire content of this article, I hope you like it.