Parse the differences between Lua and other programming languages

Source: Internet
Author: User

LuaThe difference from other programming language syntaxes is what we will introduce in this article, mainly fromLUADifferent from other programming languagesLUAFor more information about how to use the script language, see this article.

Variable does not need to declare the type (variable type), and does not need to be declared before use for the first time

The following statements are written in C language.

 
 
  1. int i;  
  2. i = 9;  
  3. printf("%d\n",i); 

Similar statements in Lua are as follows:

 
 
  1. > i = 9 
  2. > print(i)  

I and its types do not need to be declared in advance, you can directly use

In addition, the variable type is not fixed and determined by its current value. Different types of values can be assigned to the same variable at any time.

 
 
  1. > i = 9 
  2. > print(i)  
  3. 9  
  4. > i = "Hello Azeroth!" 
  5. > print(i)  
  6. Hello Azeroth! 

However, the variable type does not need to be specified during use, including number, string, Boolean, function, table, nil, thread, and userdata.

You can use the type statement to obtain the type, for example:

 
 
  1. > print(type("Hello Azeroth!"))  
  2. string 

Values can be assigned to multiple variables at the same time.

You can assign values one by one using the following methods:

 
 
  1. > x, y = 3, 5  
  2. > print(x * y)  
  3. 15 

[Non-logical operators are ~ Not! ]

 
 
  1. > print(1 ~= 3)  
  2. true 

^ The operator is a power operation, not an exclusive or operation.

That is:

 
 
  1. > print(13^2) 

You will get 169.

String operations

You can easily use the tostring ()/tonumber () function to convert string and numeric types. Scientific notation is supported.

 
 
  1. > x = tonumber("1e3")  
  2. > print(x)  
  3. 1000 

The operator "..." is used to connect strings, for example:

 
 
  1. > x = "a" .. "b"  
  2. > print(x)  
  3. ab 

In addition to double quotation marks (""), Lua also supports single quotation marks ('') or square brackets ([]) to deal with different occasions, especially when the string contains:

 
 
  1. > x = 'Hello' 
  2. > print(x)  
  3. Hello  
  4. > x = 'Isn't it nice?'  
  5. stdin:1: '=' expected near 'it' 

This sentence has an error. Lua considers the string as 'isn' and cannot interpret the subsequent parts. One solution is to use escape characters. Lua supports escape characters like other languages:

 
 
  1. > x = 'Isn\'t it nice?'  
  2. > print(x)  
  3. Isn't it nice? 

Or use square brackets:

 
 
  1. > x = [[This is a long string including ' and "]]   
  2. > print(x)  
  3. This is a long stirng including ' and " 

Matching rules of square brackets: [= .. = [String content] = .. =], in [[there can be no equal sign (in the above case), or there are any number of equal signs, the matching will look for] containing the same number of equal signs. for example, you can use the following method to include a string]:

 
 
  1. > x = [==[This is a long string including ', ]] and "]==]  
  2. > print(x)  
  3. This is a long string including ', ]] and " 

Brackets also support multi-line strings.

The string length method is similar to that of the C Shell Table. The "#" operator is used:

 
 
  1. > print(#"Hello")  

Or use the string. len () function ():

 
 
  1. > x = "Hello" 
  2. > print(string.len(x))  

Special Phenomena of logical operations

Check this statement:

 
 
  1. > print(true and 4)  

This result is different from other languages, such as C or C ++. The latter outputs true in the same case (because non-0 values are treated as true ).

Let's look at this question first: In A common logical expression A and B, obviously, if the first one, that is, A, is true, then the value of the expression is determined by B. That is, A and B. If A is true, B is true, expression A and B is true, and expression B is false.

SoLuaTrue and 4 is A logical and (A and B) expression. Because the first value is true, the value of the expression is actually determined by the second one. In other words, you only need to directly return the value of the second expression.LuaThe second expression can be returned even if no Boolean value of true or false is generated.

With this computing feature, you can reach a? in C/C ++ in Lua? Effect of the B: c statement:

 
 
  1. > print(true and "Hello" or "Azeroth!")  
  2. Hello  
  3. > print(false and "Hello" or "Azeroth!")  
  4. Azeroth 

In other words, "? B: c "statements can be written as a and B or c in Lua.

However, this statement is flawed. in a and B or c, a is usually a logical expression. B and c are not expressions. First, a and B are computed, and then (a and B) or c. under normal circumstances, if a is true, a and B will return B. If B is a string or value, it is 'true'. Therefore, B or c will return B directly, design that meets the expectation of "a is true, returns B.

However, if B is a variable and the variable is null (for example, not defined), B is false, and B or c returns c. in this case, "a is true, but c is returned.

Therefore, in order for expression a and B or c to work normally, B must be true. if B is not true, only if-else statements can be used to ensure logical rigor.

(Reference: For details about "(a and B) or c" and "? B: c "equivalent condition, by carl2500 from CWDG)

Scope of the Variable

InLuaYou can manually divide the code block using the do-end statement:

 
 
  1. > do  
  2. >> local i = 10 
  3. >> print(i)  
  4. >> end  
  5. 10  
  6. > print(i)  
  7. nil 

It is slightly different from other programming languages.

Summary: AnalysisLuaI have finished introducing the content that is different from the syntax of other programming languages. I hope this article will be helpful to you!

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.