LUA Learning Note (ii): Basic grammar

Source: Internet
Author: User

Lua Learning Guide: http://www.lua.org/manual/

The first thing we want to make clear is that in Lua everything is a variable except for the keyword.

Lua keyword

Can see this address: http://www.lua.org/manual/5.1/manual.html#2.1

About semicolons

The end of the statement can be separated by a semicolon ";" To end or not to write.

In the case of a row with multiple statements, a semicolon ";" can be used between the statements. Separated, or can be separated by blanks. In general, suggestions are always separated by semicolons.

Using annotations

Single-line comments using "--", the future content will be treated as comments;

Multiline comments begin with "--[[" and End With "]".

Variable declaration

Because it is a dynamic type, the variable declaration does not need to declare the type, but must be assigned, if you want to be a null type, you can assign nil, as follows:

1  - -- Digital 2 " ABC " -- string ABC 3 Nil -- Empty Type

Note that all variables and keywords in LUA are case-sensitive.

Assign value

You can assign values to multiple variables at once, rule: if the equal sign ("=") is more to the right, then discard, and the left is more, the assignment is empty (nil).

The expression a,b=b,a represents the value exchange of A and B, which can be exchanged in a different type.

1  - " ABC " 2 Print (A, B, c) -- ABC Nil 3 4 A, B = B, a5Print(A, B, c)--ABC Nil
Type

Let's look at what basic data types are built into Lua:

    • Nil: Indicates that the variable has not been assigned, and can be regarded as the meaning of deleting the variable;
    • Boolean: Boolean type, True and false;
    • Number: A numeric type that is internally represented by a double;
    • String: strings, which always end with 0, but can contain any character (including 0), so it is not equivalent to the C string, but its superset;
    • Table: tables, actually an associative array, the index can use numbers or strings, different from other languages of the array to 0 as the initial index of the array, the default initial index of the table in Lua is generally starting at 1. The variable of table is simply an address reference, the operation on table does not produce a copy of the table or creates a new table,table that does not have a fixed length, the table length will automatically grow when new data is added, and the table with no initial is nil;
    • Function: Functions that can be used as anonymous functions;
    • UserData: User (non-script user) defined C data structure, script user can only use it, can not be defined;
    • Thread:lua collaboration Thread (Coroutine), which is different from the preemptive thread of the general operating system;

The following notes highlight the basic data types of these cores.

And judging what type of variable to rely on is his value, let's take a look at the following example:

1num = -2str ="ABC"3obj =Nil4Func =Print5 6 Print(type(num))-- Number7 Print(type(str))--string8 Print(type(obj))--Nil9 Print(type(func))--functionTen  OneFunc =Str A  - Print(type(func))--string
Global and Local

The variables declared by default are global variables, which means they can be accessed globally, while local variables add the local keyword before the declaration, as follows:

1 Local Num 2 Local " ABC " 3 4 Print (num) -- Nil 5 Print (str) -- ABC

A global variable declaration must be assigned a value, while a local variable is not required.

Block

Do...end can declare a block, the life cycle of a local variable in its own program block and subroutine Block:

1 functionMain ()2    Do3     Localnum = -4     Print(1, num)--15      Do6       Print(2, num)--27     End8   End9   Print(3, num)--3 NilTen End One  AMain ()
Operator arithmetic operators

Include operators such as "+", "-", "*", "/", "%", but note that the self-increment decrement operator is not supported:

1 Ten 2 -- The following wording is not supported,--the direct change of the comment is 3 num++4 + +num5 num--6--num7  -- The correct wording is as follows 8191

The same shorthand form is not supported:

1 Ten 2 -- shorthand is not supported. 3 5 4 -- the correct wording is as follows 5 5

The take remainder operation supports decimals because the number type exists in double form:

1 3.14 2 1 3 Print (num) -- 0.14
Relational operators
    • = =: Judging whether it is equal;
    • ~=: Judging whether it is not equal;
    • <: Judging whether it is less than;
    • The judgment is greater than;
    • <=: Judging whether it is less than or equal;
    • >=: Judgment is greater than or equal to;

It is important to note that reference types such as table are judged using addresses:

1NUM1 =Ten2num2 =Ten3 Print(NUM1 = = num2)--true4 5STR1 ="ABC"6STR2 ="ABC"7 Print(str1 = = str2)--true8 9Table1 = {1,2,3}TenTable2 = {1,2,3} One Print(table1 = = table2)--false
logical operators
    • And: And, equivalent to the C &&;
    • Or: Or, equivalent to C's | | ;
    • Not: Non, equivalent to C in!;

We can skillfully use logical operators to implement the ternary operators in C:

1 -- the wording of C language 2 -- d = a? B:c 3 -- the Lua notation 4  and or C
Control structure If Then

Conditional Judgment statement, we directly last example:

1num =Math.random()2 Print(num)3 4 ifNum <=0.33  Then5   Print("A")6 ElseIfNum <=0.66  Then7   Print("B")8 Else9   Print("C")Ten End
While and repeat

Conditional loop statements, while is the first to judge the cycle, repeat is the first cycle to judge, examples are as follows:

1num =Ten2 3 --while the condition is that if the loop continues to be true4  whileNum ~=0  Do5num = num-16   Print(num)7 End8 9num =TenTen  One --the condition of repeat is that if it is true, exit the loop A Repeat -num = num-1 -   Print(num) the untilnum = =0
For

Loop statement;

1 --The default step size is 12  fori =0,Ten  Do3   Print(i)4 End5 6 --Specify a step size of 27  fori =0,Ten,2  Do8   Print(i)9 End
Foreach

Iterate through the statements as follows:

1Days = {"Suanday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}2 3 --I represents the index and V represents the value4  forI,vinch Ipairs(days) Do5   Print(i, v)6 End
Break and return

Usage is consistent with C, break is used to jump out of a loop, and return is used to jump out of a function.

Unsupported switch, case, and continue

Switch-case statements are not supported in Lua, we can use if ElseIf instead;

The continue statement is not supported in LUA and is implemented by referencing the following file: http://www.jb51.net/article/59099.htm

Lua adds a goto statement to the 5.2 version, which can be used to achieve the continue effect.

LUA Learning Note (ii): Basic grammar

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.