Lua Basic Syntax
Lua is very simple to learn; we can create our first Lua program!
Lua code files have the extension .lua.
The first Lua program
Interactive programming
Lua provides an interactive programming mode. We can enter programs at the command line and immediately see the results.
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 command:
> print("Hello World!")
Then we press Enter, and the output is as follows:
(
"Hello World!"
)
Hello World!
Script programming
We can save Lua program code to a file ending in lua and execute it; this mode is called script programming, such as storing the following code in a script file named hello.lua:
print("Hello World!")
print("www.runoob.com")
Execute the above script using the lua command, and the output is:
$ lua hello.lua
Hello World!
www.runoob.com
We can also modify the code to the following form to execute the script (add #!/usr/local/bin/lua at the beginning):
Example
!
/
usr
/
local
/
bin
/
lua
(
"Hello World!"
)
(
"www.runoob.com"
)
In the above code, we specified the Lua interpreter /usr/local/bin directory. The # mark causes the interpreter to ignore it. Next we add executable permission to the script and execute it:
./hello.lua
Hello World!
www.runoob.com
Notes
Single-line comment
A single-line comment begins with two hyphens -- and the content that follows is ignored by the interpreter.
--
Single-line comments are usually used for brief explanations or annotations of code.
Multi-line comment
A multi-line comment begins with --[[ and ends with ]] suitable for situations where large blocks of code need to be explained.
--[[
多行注释
多行注释
--]]
Lua does not support nested comments, which means you cannot nest another multi-line comment within a multi-line comment. For example, the following code is illegal:
--[[
这是一个多行注释
--[[
嵌套的多行注释(这是非法的)
]]
]]
Identifiers
Lua identifiers are used to define a variable, function, or other user-defined item. An identifier begins with a letter A to Z or a to z or an underscore _, followed by zero or more letters, underscores, and digits (0 to 9).
It is best not to use identifiers with an underscore followed by uppercase letters, because Lua's reserved words are also like this.
Lua does not allow special characters such as @, $, and LinkedHashMap, among others. % to define identifiers. Lua is a case-sensitive programming language. Therefore, in Lua, Runoob and runoob are two different identifiers.
The following lists some correct identifiers:
mohd zara abc move_name a_123
myname50 _temp j a23b9 retVal
Keyword
The following lists Lua's reserved keywords. Reserved keywords cannot be used as constants, variables, or other user-defined identifiers:
| and | break | do | else |
|---|---|---|---|
| elseif | end | false | for |
| function | if | in | local |
| nil | not | or | repeat |
| return | then | true | until |
| while | goto |
By convention, names beginning with an underscore followed by a series of uppercase letters (such as _VERSION) are reserved for Lua's internal global variables.
Global Variables
By default, variables are always considered global.
Global variables do not need to be declared; assigning a value to a variable creates the global variable. Accessing an uninitialized global variable does not cause an error; the result is simply: nil.
(
b
)
nil
b
=
10
(
b
)
10
If you want to delete a global variable, just assign nil to the variable.
b = nil
print(b) --> nil
Thus variable b is as if it had never been used. In other words, a variable exists if and only if it is not equal to nil.