SoFunction
Updated on 2025-03-10

Summary of basic grammar and control statements in Lua

Preface

To learn a language, the first thing is to start with the most basic grammar. This article will summarize the sentences in Lua.

Assignment

The basic meaning of assignment is to modify the value of a variable or field in a table. This is not much different from other languages. However, for Lua, there is a feature that allows "multiple assignment", that is, assign multiple values ​​to multiple variables at once, such as the following code:

Copy the codeThe code is as follows:

local x1, x2 = 2, 4
print(x1)     -->2
print(x2)     -->4

In multiple assignments, Lua first evaluates all elements to the right of the equal sign, and then performs assignments. For example, the following usage can easily exchange the values ​​of two elements:

Copy the codeThe code is as follows:

local x1, x2 = 2, 4
x1, x2 = x2, x1
print(x1)     -->4
print(x2)     -->2

Lua will always adjust the number of worthy on the right of the equal sign to be consistent with the number of the variable on the left. The rule is: if the number of worthy is less than the number of variables, then the excess variable will be assigned nil; if the number of worthy is more, then the excess value will be ignored.

Local variables and blocks

Compared with global variables, Lua also provides local variables. Create local variables through local statements:

Copy the codeThe code is as follows:

i = 10    --> Global variable
local i = 10    --> local variables

In Lua, local variables also have a scope of action, that is, if the scope of action of local variables is out, local variables will lose their function. This is the same as high-level languages ​​such as C++. During the programming process, we can also use do...end to declare a block, such as the following code:

Copy the codeThe code is as follows:

do
     local a1 = 10
     local a2 = 10
end         -->The scope of a1 and a2 ends here

As for the use of local and global variables, it is related to the programming style and actual needs, I won’t say much here.

Control structure

Almost all languages ​​have control structures, and likewise, the control structure of Lua is very simple. Lua provides if, while, repeat and for for conditional execution. All control structures have an explicit terminator: if, for and while end with end, and repeat ends with until. Note that switch structure is not supported in Lua.

if then else

If statement first tests its conditions, and then executes the then part or the else part according to the test results, the else part is optional. If you want to write nested if, you can use elseif. The following code examples will illustrate the use of if.

Copy the codeThe code is as follows:

if a < 0 then a = 0 end
if a < b then retuan a else return b end
 
if op == "+" then
     r = a + b
elseif op == "-" then
     r = a - b
elseif op == "*" then
     r = a * b
elseif op == "/" then
     r = a / b
end

while

While in Lua is the same as other languages, the sample code is as follows:

Copy the codeThe code is as follows:

local a = 10
while a > 0 do
     a = a - 1
     -- Do something else
end

repeat

repeat is like the do...while structure in C++, and the loop body will be executed at least once. The repeat-until statement repeatedly executes its loop body until the condition is true.

There are two forms of for statements in Lua: numeric for and generic for

Digital for

The syntax of numeric for is as follows:

Copy the codeThe code is as follows:

for var = exp1, exp2, exp3 do
     -- Do something
end

var changes from exp1 to exp2, and each change is incremented with exp3 as the step, and executes the code between do…end once. The third expression exp3 is optional. If not specified, Lua will default to 1. For example, the following code:

Copy the codeThe code is as follows:

for var = 1, 10 do
     print(var)
end
 
for var = 10, 1, -1 do
     print(var)
end

When using for, you need to pay attention to the following two points:

exp1, exp2 and exp3, these three expressions are valued at one time before the loop starts; they will not be evaluated every loop;

2. The control variable var will be automatically declared as a local variable of the for statement and can only be visible in the loop body.

Generics for

A generic for loop traverses all values ​​through an iterator function. In Lua's basic library, ipairs is provided, which is an iterator function for traversing arrays. From the appearance, generic for is relatively simple, but in fact it is very powerful. With different iterators, you can traverse almost everything. The standard library provides several iterators, including pairs that iterate over each line in a file, pairs that iterate over table elements, ipairs that iterate over array elements, and words that iterate over strings, etc. Of course, we can also write our own iterators. In future articles, I will summarize how to write iterators.

break and return

The break and return statements are used to jump out of the current block. The languages ​​such as break, return and C++ are the same here. The break statement is used to end a loop, and the return statement is used to return the result from a function.