LUA scripting languageGetting startedLuaThe preliminary content of the program design is described in this article. In this article, I would like to introduce howLuaProgram Design. I suppose everyone has learned at least one programming.LanguageSuch as Basic or C, especially C. BecauseLuaIs used as a script in the Host Program.
LuaThe syntax is relatively simple and easier to learn, but the function is not weak.
In Lua, everything is a variable except a keyword. Remember this sentence.
First, comment
Writing a program is always without comments.
In Lua, you can use single-line and multi-line annotations.
In a single line comment, two consecutive minus signs "--" indicate the start of the comment until the end of the line. It is equivalent to "//" in C ++ "//".
In multi-line comments, the Comment starts from "-- [" and continues. This annotation is equivalent "/*... */". In comments, "[[" and "]" can be nested.
Lua Programming
A classic "Hello world" program is always used to introduce a language. In Lua, writing such a program is simple:
- print("Hello world")
In Lua, statements can be separated by semicolons (;) or blank spaces. Generally, if multiple statements are written in the same row, we recommend that you use semicolons to separate them.
Lua has several program control statements, such:
Conditional Control: if condition then... Elseif condition then... Else... End
While loop: while condition do... End
Repeat loop: repeat... Until Condition
For Loop: for variable = initial value, end value, step do... End
For Loop: for variable 1, variable 2 ,... , Variable N in table or enumeration function do... End
Note that the for Loop Variable always acts only on the partial variable of the for, you can also omit the step value, at this time, the for Loop will use 1 as the step value.
You can use break to stop a loop.
If you have the foundation of programming, such as Basic and C, you will feel that Lua is not difficult. However, Lua has several differences from these programming languages, so pay special attention to them.
Statement Block
The statement block is enclosed by "{" and "}" in C ++. In Lua, It is enclosed by do and end. For example:
- do print("Hello") end
You can set local variables in functions and statement blocks.
Assignment Statement
The value assignment statement is enhanced in Lua. It can assign values to multiple variables at the same time.
For example:
- a,b,c,d=1,2,3,4
Even:
A, B = B, a -- how convenient the variable exchange function is.
By default, variables are always considered global. If you want to define a local variable, you must use local to describe it during the first assignment. For example:
Local a, B, c = 1, 2, 3 -- a, B, c are local variables
Numeric operation
Like the C language, it supports + ,-,*,/. But Lua has another "^ ". This indicates exponential multiplication. For example, the result of 2 ^ 3 is 8, and the result of 2 ^ 4 is 16.
Connect two strings. You can use the ".." operator. For example:
- "This a"... "string." -- equal to "this a string"
Comparison
- = == ~=
They indicate less than, greater than, not greater than, not less than, equal, not equal
All these operators always return true or false.
For Table, Function, and Userdata data, only = and ~ = Available. The two variables reference the same data. For example:
- a={1,2}
- b=a
- print(a==b, a~=b) -- true, false
- a={1,2}
- b={1,2}
- print(a==b, a~=b) -- false, true
Logical operation
And, or, not
And or differ significantly from C.
Here, remember that in Lua, only false and nil are calculated as false, and any other data is calculated as true, and 0 is also true!
The operation result of and or is not true or false, but related to its two operands.
A and B: If a is false, a is returned; otherwise, B is returned.
A or B: if a is true, a is returned; otherwise, B is returned.
For example:
- print(4 and 5) --> 5
- print(nil and 13) --> nil
- print(false and 13) --> false
- print(4 or 5) --> 4
- print(false or 5) --> 5
This is a very useful feature in Lua, and it is also a hybrid feature.
We can simulate the C language statement: x =? B: c. In Lua, it can be written as: x = a and B or c.
The most useful statement is: x = x or v, which is equivalent to: if not x then x = v end.
Operator priority. The order from high to low is as follows:
- ^
- Not-mona1 Operation)
- */+-
- .. String connection)
- = ~ ===
- And
- Or