Cc3.2 + Lua (2) -- Lua basic syntax 1

Source: Internet
Author: User
Tags arithmetic operators

[Nagging]

First, you have learned at least one host language (C ++, Java, etc.) by default, and then turn to Lua.

Because 51cto does not have code highlighting for Lua, I have to use it to make the Code look colorful. 650) This. width = 650; "src =" http://img.baidu.com/hi/face/ I _f32.gif "alt =" I _f32.gif "/>


This section only introduces the basic simple Syntax of Lua. For more instructions, see the Lua Chinese tutorial and Lua reference manual.


[Learning emotion]

> No extra points are required at the end of the statement;

> The statement block does not use braces {}, but the DO statement block end indicates the start and end of the statement block.

Braces {} represent a table structure.

> Operator ^: indicates a power operation. (2 ^ 10 = 1024)

> Not equal :~ =.

> Data type is not required for variable definition. Direct A = 10.

> And the return values of (and), or (OR) operations are not true/false. The variable value on the left or right of the operator.

> Except nil and false, all others are true. (0 and empty string "" are also true ).

> Variable values can be assigned together. (A, B, C = 10, 20, 30 ).

> The function can return multiple values. (Return a, B, c)

> In Lua, there is only one data structure type: Table ).

It can represent an array, a data structure, or a class.

> The subscript of the array starts from 1, instead of 0.



0. Lua environment Configuration

You can goLua Official WebsiteDownload the resource file from www.lua.org and write it in a text editor.

You can also useEclipse, DownloadLDT Plug-in to compile Lua code in eclipse.


0. Lua example

Let's take a look at a Lua example. The figure is fresh. 650) This. width = 650; "src =" http://img.baidu.com/hi/face/ I _f07.gif "alt =" I _f07.gif "/>

Note: The following code can be run directly. Yes. The main function is not required!

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4C/C1/wKiom1RE46ywaKPZAAKGP3sK2ew218.jpg "Title =" 11.png" alt = "wkiom1re46ywakpzaakgp3sk2ew218.jpg"/>


1. Data Type

> When defining a variable in Lua,No data type to write.

> Lua is defined as follows: a = 10; B = "Hello World ".

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4C/BA/wKioL1REiKywadAjAAJZDtvFBIo814.jpg "Title =" 1.png" alt = "wkiol1reikywadajaajzdtvfbio814.jpg"/>


2. Notes

(1) single line comment:--

(2) multi-line comment:-- [[--]

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4C/BC/wKioL1REq5ODiclgAABMd1oJpmY804.jpg "Title =" 2.jpg" alt = "wkiol1req5odiclgaabmd1ojpmy804.jpg"/>


3. Output statement

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4C/BC/wKioL1RErZqSoegcAAFyH41AcdA898.jpg "Title =" 3.jpg" alt = "wkiol1rerzqsoegcaafyh41acda898.jpg"/>


4. reserved keywords

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4C/BC/wKiom1REriSCpRN9AADAUcjLjjo203.jpg "Title =" 4.png" alt = "wkiom1reriscprn9aadaucjljjo203.jpg"/>


5. Operators


5.1 Arithmetic Operators

> Where^IsPower Operation.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4C/BD/wKiom1REtADSqm0IAABnGBQARuI593.jpg "Title =" 11.jpg" alt = "wkiom1retadsqm0iaabngbqarui593.jpg"/>


5.2 Relational operators

> Where~ =IsNot equal.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4C/BD/wKioL1REtFyw-BHpAACoyv31I_I769.jpg "Title =" 22.png" alt = "wKioL1REtFyw-BHpAACoyv31I_I769.jpg"/>


5.3 logical operators

> The logical operators in Lua do not return true/false, but A or B.

> The Three-object operator can be expressed as follows.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4C/BD/wKioL1REtGvCl6jDAADZm7_VAfA454.jpg "Title =" 33.png" alt = "wkiol1retgvcl6jdaadzm7_vafa454.jpg"/>


5.4 Concatenation Operators

>Two points ..String concatenation.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4C/BD/wKioL1REtSKQCJQRAAA9vX1Ynjw599.jpg "Title =" 44.png" alt = "wkiol1retskqcjqraaa9vx1yn213599.jpg"/>


5.5 priority

> ^ And... are right links, and the rest are calculated from left to right.

> Priority: 650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4C/BD/wKiom1REtRuTI3ChAACXlKBRKOM887.jpg "Title =" 55.png" alt = "wkiom1retruti3chaacxlkbrkom887.jpg"/>


6. Variable assignment and scope


(1) local variables (Local), Global variables.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4C/BD/wKiom1REtbeQjnsCAACt9jyCERk129.jpg "Title =" a.jpg "alt =" wkiom1retbeqjnscaact9jycerk129.jpg "/>


(2) multi-value assignment

> The left side of the equal sign is the variable, and the right side is the assigned value. Values are assigned in sequence.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4C/BE/wKiom1REvYbxFfm7AAGFXpOZZok917.jpg "Title =" I .png "alt =" wkiom1revybxffm7aagfxpozzok917.jpg "/>


7. Code Block

> UseDo end.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4C/BD/wKiom1REti2jdARJAACmTkAAEks689.jpg "Title =" B .png "alt =" wkiom1reti2jdarjaacmtkaaeks689.jpg "/>


8. control statements

Table knowledge may be involved here, which will be introduced later.


 8.1 conditional statement if-else-then

>ElseifThere is no space in the middle.

> Don't forgetThen.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4C/BE/wKioL1REt5vjSr9sAACm6F6bj3U023.jpg "Title =" q.png "alt =" wkiol1ret5vjsr9saacm6f6bj3u023.jpg "/>


 8.2 loop statement while-do

> The usage is similar to that in C ++.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4C/BE/wKioL1REuFfwgS39AAB5EUP3OYg234.jpg "Title =" 20.png "alt =" wkiol1reuffwgs39aab5eup3oyg234.jpg "/>


 8.3 loop statement repeat-

> Similar to do-while in C ++.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4C/BD/wKiom1REuRjyIg1GAABwKH4LbPY025.jpg "Title =" e.jpg "alt =" wkiom1reurjyig1gaabwkh4lbpy025.jpg "/>


 8.4. Loop statement for-do

The for loop statement is the most commonly used in Lua.


(1) usage 1: Number Traversal

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4C/BE/wKioL1REu26j_03tAAEdP9z276s735.jpg "Title =" r.png "alt =" wkiol1reu26j_03taaedp9z276s735.jpg "/>


(2)Usage 2: array Traversal

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4C/BE/wKioL1REu32DwPOlAADi1wo40Uc298.jpg "Title =" rr.png "alt =" wkiol1reu32dwpolaadi1wo40uc298.jpg "/>


(3) usage 3: array traversal (ipairs)

> UseFor key, VAR in ipairs (t) Do end

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4C/BE/wKioL1REvwLDLNYPAAFS6xs62PQ762.jpg "Title =" rrrr.png "alt =" wkiol1revwldlnypaafs6xs62pq762.jpg "/>


(4) usage 4: Table traversal (pairs)

> Traverse the entire table and output all the data in the table.

> UseFor key, VAR in pairs (t) Do end

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4C/BF/wKioL1REv7-CYI5kAAC9jO5UOsI389.jpg "Title =" rrrrr.png "alt =" wKioL1REv7-CYI5kAAC9jO5UOsI389.jpg "/>


(5) differences between ipairs and Pairs

>Ipairs: Mainly used for table array traversal, that is, the table subscript is a positive integer, and from 1 ~ N consecutive.

>Pairs: Used to traverse the entire table.


9. Function

The function is defined as follows:

Function Name (parameter list) Statement block end

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4C/BF/wKioL1REwaaSSiI2AABlx762WZ4675.jpg "Title =" z.png "alt =" wkiol1rewaassii2aablx762wz4675.jpg "/>


 9.1 variable parameters

At the end of the function parameter list, useThree points ...The input parameter is variable.

Variable parameters are stored in an array of tables.ARG.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4C/BE/wKiom1REwnbw-gF3AAE6OCcm7bQ161.jpg "Title =" zz.png "alt =" wKiom1REwnbw-gF3AAE6OCcm7bQ161.jpg "/>


 9.2 multiple return values

The function can return multiple values.

Example:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4C/BF/wKioL1REw3nQbqqKAAHRKAlL0iQ038.jpg "Title =" zzz.jpg "alt =" wkiol1rew3nqbqqkaahrkall0iq038.jpg "/>


10. Table)

There is only one data structure in Lua: Table ).

Principle:Key-value pair.


 10.1 represents an array

Prerequisites:The subscript of the array is a positive integer and the value is continuously assigned.

And in Lua,Array subscript from1Start.(T [1], t [2], t [3]...)

Example:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4C/C0/wKioL1REyTPB33zfAAI60I_FeIQ096.jpg "Title =" x.png "alt =" wkiol1reytpb33zfaai60i_feiq096.jpg "/>


 10.2 Table usage

(1) initialization assignment

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4C/BF/wKiom1REzaWAsYJRAAHRvCnW2tI494.jpg "Title =" cc.png "alt =" wkiom1rezawasyjraahrvcnw2ti494.jpg "/>


(2) usage of table members

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4C/C0/wKioL1REztzR8hPSAACloajYwVE540.jpg "Title =" ccc.png "alt =" wkiol1reztzr8hpsaacloajywve540.jpg "/>



The basic syntax is finished. Please wait for the next decomposition...


This article is from the "summer wind" blog, please be sure to keep this source http://shahdza.blog.51cto.com/2410787/1565974

Cc3.2 + Lua (2) -- Lua basic syntax 1

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.