Lua Learn about the operators and expressions of notes _lua

Source: Internet
Author: User

This blog learns about the LUA operator, which is simpler, and I write the instructions directly to the code, as follows.

--Arithmetic operator--two-dollar operator: +-*/^ (subtraction power)--Unary operator:-(negative)-the operands of these operators are real numbers, and there are no self-decrement operators in Lua. --Relational operator--< > <= >= = = ~=--Here's what you need to be aware of, which is not represented by ~=--these operators return the results to false or true;== and ~= compare two values, and Lua thinks they are different if two value types are different;- The-nil is only equal to himself. Lua compares tables, UserData, and functions by reference.
That is, it is equal if and only if both represent the same object.  --lua Compare numbers by traditional numeric size, comparing strings in alphabetical order, print ("0" = 0)--false A = {1,2} b = {1,2} c = a print (a==c)-->true print (a==b) -->false Print (2 <)--True Print ("2" < ")--false--logical operator--and or not--logical operators think false and nil are false (false)
, the other is true, and 0 is true.
The--and and OR operation results are not true and false, but are related to its two operands.
--a and B Returns a If a is false, otherwise returns B--a or B if A is true, returns a, otherwise returns B print (false)--> false print (4 or 5)--> 4 Print (False or 5)--> 5-A useful technique: ternary operator A in C language? B:C can be implemented in LUA: (A and B) or C local A = 1 Local b = 2 Local num = (a>b and a) or B print (num)-->2--concatenation operator--.. Two points-string concatenation, and Lua converts numbers to strings if operands are numbers. Notice the space between the numbers print ("Hello" ...).

 Lua ")--> Hello Lua print (0.. 1)--> 01

Finally explain the precedence of operators, give you a table for reference, use the time to check.

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.