Basic syntax and control statement summarization in Lua _lua

Source: Internet
Author: User

Objective

Learning a language begins with the most basic grammar, which is a summary of the statements in Lua.

assigning values

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

Copy Code code as follows:

Local x1, x2 = 2, 4
Print (x1)-->2
Print (x2)-->4

In multiple assignments, LUA first evaluated all the elements to the right of the equals sign before performing the assignment, such as the following usage, which makes it easy to swap the values of two elements:

Copy Code code as follows:

Local x1, x2 = 2, 4
x1, x2 = x2, x1
Print (x1)-->4
Print (x2)-->2

Lua always adjusts the number on the right side of the equal sign to the number of left variables, and the rule is: if the number of values is less than the number of variables, then the extra variable is nil; if the number is worth more, then the extra value will be ignored.

Local variables and blocks

LUA also provides local variables relative to global variables. To create a local variable from a regional statement:

Copy Code code as follows:

i = Ten--> global variable
Local i = Ten--> locals

In Lua, local variables also have scope, that is, the scope of local variables, local variables will be lost, this and C + + and other high-level languages are the same. In the process of programming, we can also use Do...end to display a block of declarations, such as the following code:

Copy Code code as follows:

Todo
Local A1 = 10
Local A2 = 10
End-->A1 and A2 scopes are over

As for the use of local variables and global variables, related to the programming style and practical needs, there is no more to say.

Control structure

Almost all languages have control structures, and similarly, the control structure of LUA is very simple. LUA provides the IF, loop-while, repeat, and for-for-condition execution. All control structures have an explicit terminator: if, for, and while ending with end, repeat ends with until. It is particularly noted that the switch structure is not supported in Lua.

If then else

The IF statement tests its condition and then executes the then part or else part based on the test results, or else is optional. If you want to write nested if, you can use ElseIf, and the following code example is used to illustrate the use of If.

Copy Code code as follows:

If a < 0 then a = 0 end
If a < b then retuan a else return b

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 in other languages, and the sample code is as follows:

Copy Code code 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 executes at least once. The Repeat-until statement repeats its loop body until the condition is true.

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

Digital for

The syntax for the numeric for IS as follows:

Copy Code code 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 length, and the code between do...end is executed once. The third expression Exp3 is optional, and Lua defaults to 1 if unspecified. For example, the following code:

Copy Code code as follows:

for var = 1
Print (VAR)
End

for var = ten, 1,-1 do
Print (VAR)
End

When using for, you need to be aware of the following two points:

1.for EXP1,EXP2 and EXP3, these three expressions are evaluated at one time before the start of the loop, and are not evaluated at every loop;

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

Generics for

A generic for loop traverses all values through an iterator function. Ipairs is provided in Lua's underlying library, which is an iterator function for traversing arrays. Generics for the appearance of simpler, but in fact it is very powerful. With a different iterator, you can almost iterate through everything. The standard library provides several iterators, including the io.lines for each row in the iteration file, the pairs of the iteration table element, the ipairs of the iterative group elements, and the string.gmatch of the words in the iteration string. Of course, we can also write our own iterators, and in future articles, I'll summarize how to write iterators.

Break and return

The

Break and return statements are used to jump out of the current block. The break, return, and C + + languages are the same here. The break statement ends a loop in which the return statement returns the result from a function.

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.