Variable is a name given to a storage area which our programs can manipulate. It can hold different types of values including functions and tables.
Variable name is a collection of letters, digits and underscore character. It should begin with either a letter or an underscore. Upper and lowercase letters are different as Lua is case-sensitive. Lua has eight basic types of values
Lua does not have variable data types and there are only three types based on the scope of the variable.
A variable definition in Lua informs the interpreter where and how much to create the storage for a variable. A variable definition has an optional type and it consists of a list of one or more variables of that type as shown below
Here, type is optionally local or type specified making it global and variable_list may contain one or more identifier names separated by commas. Some valid declarations are shown here
Line local i, j both are used for declaring and defining the variables i and j; which inform the interpreter to create variables named i, j and limits the scope to be local.
Variables can be initialized (assigned an initial value) in their declaration. Initializer will consist of an equal sign followed by a constant expression as shown below
Some examples are
For definition without an initializer − variables with static storage duration are implicitly initialized with nil.
In the above examples, assignments for multiples variables will follow a variable_list and value_list format. In the above example local d, f = 5,10 we have d and f in variable_list and 5 and 10 in values list.
Value assigning in Lua will take place like first variable in the variable_list with first value in the value_list and so on. Hence, value of d is 5 and value of f is 10.
Try below example, where variables are declared at the top, but they have been defined and initialized inside main function
When the above code is built and executed, it produces below result
Lua has two kinds of expressions
Variables are lvalues and they can appear on the left-hand side of an assignment. Numeric literals are rvalues and they will not be assigned and cannot appear on the left-hand side. Below is a valid statement
But below shown is not a valid statement and it might generate a build-time error
In Lua programming language, apart from the above types of assignment, it is possible to have multiple lvalues and rvalues in the same single statement. It is shown below.
In the above statement, 20 is assigned to g and 30 is assigned to l.
Lua (programming language) Related Interview Questions
|Python Interview Questions||HTML 5 Interview Questions|
|ASP.NET Interview Questions||Oracle 11g Interview Questions|
|C & Data Structures Interview Questions||Basic Programming Interview Questions|
|Six Sigma Interview Questions||SQL Interview Questions|
|Java Abstraction Interview Questions||Data Structure & Algorithms Interview Questions|
|Computer Programming Interview Questions||PMP Interview Questions|
|.Net Deployment Interview Questions|
Lua (programming Language) Tutorial
Lua (programming Language)
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.