Expression of Lua learning Notes

Source: Internet
Author: User

Expression of Lua learning Notes

1. Arithmetic operators

Binary operator: +-*/^ (addition, subtraction, multiplication, and Division power)

Unary operator:-(negative value)

The operations of these operators are real numbers.

2. Relational operators

<,>, <=, >=, = ~ =

These operators return false or true; = and ~ = Compare two values. If the two values have different types, lua considers them different. nil is only equal to itself. Lua compares tables, userdata, and funcations by referencing. That is to say, if and only if the two indicate that the same object is equal.

Lua compares numbers with traditional numbers and compares strings in alphabetical order, but the alphabetic order depends on the local environment.

Pay special attention to comparing different types of values;

"0" = 0-false

2 <15-true

"2" <"15"-false to avoid inconsistent results, lua will report an error when comparing numbers and strings, for example, 2 <"15"

3. Logical operators

And or not

Logical operators think that false and nil are false, others are true, and 0 is true.

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 (4and 5) --> 5

Print (niland 13) --> nil

Print (falseand 13) --> false

Print (4or 5) --> 4

Print (falseor 5) --> 5

Useful tips: assign an initial value v to x if x is false or nil.

X = x or v

Equivalent

If not x then

X = v

End

And has a higher priority than or.

Ternary operators in C language:

A? B: c

In lua, you can implement the following:

(A and B) or c

Not always returns false or true

Ptint (notnil) --> truue

Print (notfalse) --> true

Print (not 0) --> false

Pirint (not notnil) --> false

4. Join operators

... Two points

String connection. The cleavage operand is a number, and Lua converts the number into a string.

Print ("hello"... "world ")

5. Priority

Sorting from high to low:

^

Not-(unary)

*/

+-

..

<> <=> = ~ ===

And

Or

All binary operators except ^ and... are connected.

A + I <B/2 + 1 <--> (a + I) <(B/2) + 1)

5 + x ^ 2*8 <--> 5 + (x ^ 2) * 8)

6. Table structure

The constructor is the expression used to create and initialize tables. It indicates something unique to lua that is powerful. The simplest constructor is {}, which is used to create a blank table. You can directly initialize the array:

Days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "staturday "}

Lua initializes Sunday in days [1] (the first element index is 1) and uses Monday to initialize days in days [2].

Print (days [4]) --> Wednesday

Constructors can use any expression to initialize:

Tab = {sin (1), sin (2), sin (3), sin (4), sin (5), sin (6), sin (7 ), sin (8 )}

 

 

Expression of Lua learning Notes

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.