Introduction to LUA scripting language preliminary design of Lua Program (1)

Source: Internet
Author: User

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:

 
 
  1. 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:

 
 
  1. 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:

 
 
  1. 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:

 
 
  1. "This a"... "string." -- equal to "this a string"

Comparison

 
 
  1. = == ~= 

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:

 
 
  1. a={1,2}  
  2. b=a 
  3. print(a==b, a~=b) -- true, false  
  4. a={1,2}  
  5. b={1,2}  
  6. 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:

 
 
  1.  print(4 and 5) --> 5  
  2.  print(nil and 13) --> nil  
  3.  print(false and 13) --> false  
  4.  print(4 or 5) --> 4  
  5.  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:

 
 
  1. ^
  2. Not-mona1 Operation)
  3. */+-
  4. .. String connection)
  5. = ~ ===
  6. And
  7. Or


Related Article

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.