Lua Basic Syntax
Learning Lua is very straightforward; we can create our first Lua program!
First Lua Program
Interactive Programming
Lua provides an interactive programming mode. We can type programs in the command line and see the effects immediately.
The 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
>
In the command line, enter the following command:
> print("Hello World!")
Then press Enter, and the output will be:
> print("Hello World!")
Hello World!
>
Script-based Programming
We can save Lua program code in a file ending with .lua
and execute it. This mode is called script-based programming. For example, we store the following code in a script file named hello.lua
:
print("Hello World!")
print("www.tutorialpro.org")
Executing the script with the lua
command gives the output:
$ lua hello.lua
Hello World!
www.tutorialpro.org
We can also modify the code to execute the script by adding #!/usr/local/bin/lua
at the beginning:
Example
#!/usr/local/bin/lua
print("Hello World!")
print("www.tutorialpro.org")
In the above code, we specify the Lua interpreter in the /usr/local/bin
directory. The #
symbol marks the interpreter and will be ignored. Next, we add execute permissions to the script and run it:
./hello.lua
Hello World!
www.tutorialpro.org
Comments
Single-line Comments
Two hyphens are used for single-line comments:
--
Multi-line Comments
--[[
Multi-line comment
Multi-line comment
--]]
Identifiers
Lua identifiers are used to define variables, functions, or other user-defined items. Identifiers start with a letter (A-Z or a-z) or an underscore _
followed by zero or more letters, underscores, or digits (0-9).
It is best not to use identifiers with an underscore followed by uppercase letters, as Lua's reserved words follow this pattern.
Lua does not allow special characters such as @
, $
, and %
in identifiers. Lua is a case-sensitive programming language. Therefore, tutorialpro
and tutorialpro
are considered different identifiers. Here are some valid identifiers:
mohd zara abc move_name a_123
myname50 _temp j a23b9 retVal
Keywords
The following are 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, identifiers starting with an underscore followed by uppercase letters (like _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 will not result in an error; instead, it returns nil
.
> print(b)
nil
> b=10
> print(b)
10
>
To delete a global variable, simply assign nil
to it:
b = nil
print(b) --> nil
This makes the variable b
seem as if it was never used. In other words, a variable exists if and only if it is not nil
.