Lua Basic Syntax

Lua is very simple to learn, and we can create our first Lua program!

The file extension for Lua code files is.lua。


First Lua Program

Interactive Programming

Lua provides an interactive programming mode. We can enter programs at the command line and see the results 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 command:

> print("Hello World!")

Then press Enter, and the output is as follows:

> print("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. For example, we store the following code in a script file named `hello.lua`:

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

Execute the above script with the `lua` command, and the output is:

$ lua hello.lua
Hello World!
www.example.com

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

Examples

#!/usr/local/bin/lua

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

In the above code, we specified Lua's interpreter `/usr/local/bin/lua`. Adding the `#` mark tells the interpreter to ignore it. Next, we add executable permissions to the script and execute it:

./hello.lua 
Hello World!
www.example.com

Comments

Single-line comment

A single-line comment starts with two hyphens--at the beginning, and the content following it will be ignored by the interpreter.

--

Single-line comments are usually used for brief explanations or markers of code.

Multi-line comment

Multi-line comments start with--[[and end with]]suitable for cases where a large block of code needs to be described.

--[[
 Multi-line comment
 Multi-line comment
 --]]

Lua does not support nested comments, which means you cannot nest another multi-line comment inside a multi-line comment. For example, the following code is invalid:

--[[
This is a multi-line comment
--[[
Nested multi-line comment (this is illegal)
]]
]]

Identifiers

Lua identifiers are used to define a variable, function, or other user-defined items. An identifier starts with a letter A to Z or a to z or an underscore_followed by zero or more letters, underscores, or digits (0 to 9).

It is best not to use identifiers consisting of an underscore followed by uppercase letters, because Lua's reserved words are also like that.

Lua does not allow special characters such as@, $, and%to define identifiers. Lua is a case-sensitive programming language. Therefore, in Lua, Example and example are two different identifiers.

The following lists some correct identifiers:

mohd         zara      abc     move_name    a_123
myname50     _temp     j       a23b9        retVal

Keywords

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 general convention, names starting with an underscore followed by a string 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 this global variable. Accessing an uninitialized global variable does not cause an error; it simply results in: `nil`.

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

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

b = nil
print(b)      --> nil

In this way, variable `b` is as if it has never been used. In other words, a variable exists if and only if it is not equal to `nil`.

Other extensions