Basic Lua language summary (3) -- Statements

Source: Internet
Author: User

Assignment

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

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

 

In multi-value assignment, Lua evaluates all the elements on the right of the equal sign before assigning values. For example, in the following usage, the values of the two elements can be easily exchanged:

 

 

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

Lua always adjusts the number of values on the right of the equal sign to be consistent with the number of variables on the left. The rule is: if the number of values is less than the number of variables, extra variables will be assigned as nil; if there are more values, the extra values will be ignored.

 

Local variables and blocks

Compared with global variables, Lua also provides local variables. Use the local statement to create a local variable:

12 I = 10 --> global variablesLocal I = 10 --> local variable

 

In Lua, local variables also have a range of effects. That is to say, when the range of local variables is reached, local variables will become ineffective, this is the same as C ++ and other advanced languages. In the programming process, we can also use do... End to declare a block, such as the following code:

1234 do     local a1 = 10      local a2 = 10 End --> the scope of A1 and A2 ends here.

 

The use of local variables and global variables is related to the programming style and actual needs.

 

Control Structure

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

 

If then else

The IF statement tests the conditions first, and then executes the then or else part based on the test results. The else part is optional. If you want to write nested if, you can use elseif. The following code example shows how to use if.

123456789101112 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

The while in Lua is the same as that in other languages. The sample code is as follows:

12345 local a = 10 while a > 0 do     a = a - 1      -- Do something elseend

 

Repeat

Repeat is like do in C ++... While structure, the loop body will be executed at least once. The repeat-until statement repeats the execution of its loop body until the condition is true.

 

There are two for statements in Lua: Numeric for and generic.

 

Digital

The syntax of numeric for is as follows:

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

 

VaR changes from exp1 to exp2. Each change increases progressively with exp3 as the step size and executes do... End code. The third expression exp3 is optional. If not specified, Lua sets the step size to 1 by default. For example, the following code:

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

 

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

1. For exp1, exp2, and exp3, the three expressions are evaluated at one time before the start of the loop;

2. The control variable VAR is automatically declared as a local variable of the for statement and is only visible in the body of the loop.

 

Generic

A generic for loop uses an iterator function to traverse all values. Ipairs is provided in the base library of Lua, which is an iterator function used to traverse arrays. In terms of appearance, generic for is relatively simple, but it is actually very powerful. Different iterators can traverse almost everything. The standard library provides several iterators, including Io for each row in the iteration file. lines, pairs of the iterative table element, ipairs of the iterative array element, and string of the word in the iterative string. gmatch. Of course, we can also write our own iterator. In future articles, I will summarize how to compile the iterator.

 

Break and return

The break and return statements are used to jump out of the current block. Here, the break, return, and C ++ languages are the same. The break statement is used to end a loop, and the return statement is used to return results from a function.

 

Source: http://www.jellythink.com/archives/489

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.