Lua learning (2)--Lua basic grammar

The first Lua program

interactive programming

Lua provides an interactive programming mode. We can enter the program at the command line and see the effect immediately.

Lua interactive programming mode can be enabled with the command lua -i or lua :

$ lua -i 
$ Lua 5.3.0  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> 

At the command line, enter the following commands:

> print("Hello World!")

Then we press the Enter key and the output is as follows:

> print("Hello World!")
Hello World!
> 

scripted programming

Keep the Lua program code to a file ending with lua and execute it. This mode is called script programming. For example, we store the following code in a script file named hello.lua:

print("Hello World!")
print("www.runoob.com")

Execute the above script using the lua name, and the output is:

$ lua test.lua
Hello World!
www.runoob.com

We can also modify the code to execute the script as follows (add at the beginning: #!/usr/local/bin/lua):

#!/usr/local/bin/lua

print("Hello World!")
print("www.runoob.com")

In the above code, we specified the Lua interpreter /usr/local/bin directory. Marking it with a pounder will ignore it. Next we add executable permissions to the script and execute:

./test.lua 
Hello World!
www.runoob.com

Notes

single line comment

Two minus signs are single-line comments:

--

multi-line comment

--[[ 多行注释 多行注释 --]]

Identifier

Lua identifiers are used to define a variable, and functions get other user-defined items . Identifiers start with a letter A to Z or a to z or an underscore _ followed by zero or more letters, underscores, numbers (0 to 9).
It's best not to use underscore-plus-case identifiers , as Lua's reserved words are the same.
Lua does not allow the use of special characters such as @, $, and % to define identifiers . Lua is a case-sensitive programming language . So in Lua Runoob and runoob are two different identifiers. Some of the correct identifiers are listed below:

myname50     _temp     j       a23b9        retVal

Key words


and         break       do      else
elseif      end         false   for
function    if          in      local
nil         not         or      repeat
return      then        true    until
while

As a general convention, names starting with an underscore followed by a string of uppercase letters (eg _VERSION ) are reserved for Lua internal global variables.

global variable

By default, variables are always considered global.
Global variables do not need to be declared. After assigning a value to a variable, the global variable is created , and there is no error in accessing an uninitialized global variable, but the result is: nil.

> print(b)
nil
> b=10
> print(b)
10
> 

If you want to delete a global variable, just assign the variable to nil.

b = nil
print(b)      --> nil

This makes variable b appear as if it were never used. In other words, a variable exists if and only if it is not equal to nil.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325588441&siteId=291194637