Lua Basic Syntax
# Lua Basic Syntax
Lua is very simple to learn, and we can create our first Lua program!
Lua code files have the extension .lua.
* * *
## First Lua Program
### Interactive Programming
Lua provides an interactive programming mode. We can enter programs in the command line and see the results immediately.
Lua's 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 we press the Enter key, and the output is as follows:
>print("Hello WorldοΌ")
Hello WorldοΌ
>
* * *
## Script-based Programming
We can save Lua program code into 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..com")
Use the `lua` command to execute the above script, and the output is:
$ lua hello.lua Hello WorldοΌ www..com
We can also modify the code to execute the script in the following form (add at the beginning: `#!/usr/local/bin/lua`):
## Example
#!/usr/local/bin/lua
print("Hello WorldοΌ")
print("www..com")
In the above code, we specified the Lua interpreter in the `/usr/local/bin` directory. The `#` sign tells the interpreter to ignore it. Next, we add executable permissions to the script and execute it:
./hello.lua Hello WorldοΌ www..com
* * *
## Comments
### Single-line Comments
Single-line comments start with two hyphens `--`, and the content following them will be ignored by the interpreter.
--
Single-line comments are usually used for brief explanations or markers in the code.
### Multi-line Comments
Multi-line comments start with `--[[` and end with `]]`, suitable for explaining large blocks of code.
--[]
Lua does not support nested comments, which means another multi-line comment cannot be nested 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 variables, functions, or 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, or digits (0 to 9).
It is best not to use identifiers starting with an underscore followed by uppercase letters, as Lua's reserved words also follow this pattern.
Lua does not allow special characters like **@**, **$**, and **%** to be used in identifiers. Lua is a case-sensitive programming language. Therefore, in Lua, `` and `` are two different identifiers.
The following are 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 convention, names starting with an underscore followed by a string of uppercase letters (e.g., `_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 returns the result: `nil`.
>print(b)
nil
> b=10
>print(b)
10
>
If you want to delete a global variable, simply assign the variable to `nil`.
b = nilprint(b) --> nil
This makes the variable `b` as if it was never used. In other words, a variable exists if and only if it is not equal to `nil`.
YouTip