Lua Programming basic grammar

Lua basic grammar

Lua learning is very simple, we can create the first Lua program!

The first Lua program

Interactive programming

Lua provides an interactive programming mode. We can enter the program on the command line and see the effect immediately.
Lua interactive programming mode can be enabled by command lua -i or lua:
$ Lua - i
Lua 5.3 . 0 Copyright ( C ) 1994 - 2015 Lua . Org , PUC - Rio >
On the command line, enter the following command:
> Print ( "Hello World!" ) 
Then we press the Enter key, the output is as follows:
> Print ( "Hello World!" ) Hello World ! > 

Scripted programming

We can keep the Lua program code into a lua-terminated file and execute it, called scripting, as we store the following code in a script file named hello.lua:
Print ( "Hello World!" ) Print ( "" )
Use lua name to perform the above script, the output is:
$ Lua test . Lua
Hello World !
. W3cschool . Cc
We can also modify the code to execute the script as follows (at the beginning: #! / Usr / local / bin / lua):
#! / Usr / local / bin / lua

Print ( "Hello World!" ) Print ( "" )
In the above code, we specified Lua’s interpreter / usr / local / bin directory. Plus the # sign interpreter will ignore it. Next we add executable permissions for the script and execute:
./ test . Lua 
Hello World !
. W3cschool . Cc


Single line comment

The two minus signs are single-line comments:

Multi-line comment

- [[ multi-line comment multi-line comment -]]


The Lua notation is used to define a variable, and the function gets other user-defined items. The identifier is followed by a letter A to Z or a to z or underlined _ followed by 0 or more letters, underscore, numbers (0 to 9).
It is best not to use underscore to increase the letter of the letter, because Lua’s reserved word is 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 W3c and w3c are two different identifiers. Here are some correct tags:
Mohd zara abc move_name a_123
Myname50 _temp j a23b9 retVal

Key words

Lua’s reserved keywords are listed below. Reserved keywords can not be used as constants or variables or other user-defined tags:
And Break Do Else
Elseif End False For
Function If In Local
Nil Not Or Repeat
Return Then True Until
In general, the name of a string of capital letters (such as _VERSION) at the beginning of an underscore is reserved for the Lua internal global variable.

Global variable

By default, variables are always considered global.
Global variables do not need to be declared, after the assignment of a variable to create the global variable, access to a global variable is not initialized will not go wrong, 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 
So the variable b seems to have never been used the same. In other words, this variable is present if and only if a variable is not equal to nil.

Leave a Reply

Your email address will not be published. Required fields are marked *