Lua Programming
Language
Agenda
• Introduction to Lua
• Data Types in Lua
• Reserved Words & Variables
• Operators
• Control Statements
• Functions
2
Introduction to Lua
What is Lua
and why to use
Lua?
What is Lua?
• Lua is a powerful, efficient,
lightweight, embeddable scripting
language. It supports procedural
programming, OOP, functional
programming, data driven
programming etc.
4
4
Advantages of Lua
Powerful Fast
• Lua is a very beginner friendly language and
is easy to implement.
• Lua is not a pure object-oriented language,
yet it provide meta-mechanisms for
implementing classes and other OOP
concepts.
• Lua has a safe environment, automatic
memory management, and good facilities for
handling strings and other kinds of data with
dynamic size.
• Lua has a deserved reputation for
performance.
• To claim to be ‘as fast as Lua’ is an
aspiration of other scripting
languages.
5
Advantages of Lua
Portable Embeddable
Lua runs on:
• All flavors of Unix
• Windows
• On mobile devices (running Android,
iOS, Windows Phone)
• Embedded microprocessors (ARM and
Rabbit) and even on IBM mainframes.
• Lua has a simple and well documented
API that allows strong integration with
code written in other languages.
• C, C++, C#, Java, Perl, Ruby
6
Data Types in Lua Data types
available in Lua
Data Types
• Lua is a dynamically typed language.
• There are no type definitions in the language; each value carries its own type.
• Values ​​can be stored in variables, passed as a parameter or return the results.
• Lua has eight basic types are: nil, Boolean, number, string, userdata, function, thread
and table.
8
Example
9
Data Types in Lua
nil as a kind of non-value, to represent the absence of a useful value (equivalent to false in conditional expressions).
nil
The Boolean type has two values, false and true.
Boolean
The number type represents real (double-precision floating-point) numbers.
Number
Strings have the usual meaning: a sequence of characters.
String
The table type implements associative arrays, the index of the array can be a number or a string.
Tables
Functions can be stored in variables, passed as arguments to other functions, and returned as results.
Functions
10
Tables
• The table type implements associative arrays.
• An associative array is an array that can be indexed not only
with numbers, but also with strings or any other value of the
language, except nil.
• Moreover, tables have no fixed size; you can add as many
elements as you want to a table dynamically.
• Also, you can assign nil to a table field to delete it.
11
Tables
• To create a table a table constructor expression is used.
• In its simplest form it is written as {}.
• To access elements of a table [] is used.
12
Nested Tables
• It is possible to implement nested tables in Lua.
• To do so simply assign tables as the elements of another
table.
• To access the tables inside tables [][] is used.
13
Example
14
Table Manipulation
• Insertion and removal of items in tables is most common in
table manipulation.
15
Inserts a value into the table at specified position.
table.insert
(table, pos,
value)
Removes the value from the table.
table.remove
(table , pos)
Example
16
Reserved Words &
Variables
Variable
naming and
reserved words
Variable names or Identifiers
Identifiers in Lua can be any string of letters, digits, and
underscores, not beginning with a digit or other special
characters; for instance:
i player MAX_health
_ij aVeryLongname _INPUT
18
Global Variables
• Global variables do not need declarations. You simply assign
a value to a global variable to create it.
• It is not an error to access a non-initialized variable; you just
get the special value nil as the result.
19
Global Variables
• Usually, you do not need to delete global variables; if your variable
is going to have a short life, you should use a local variable.
• But, if you need to delete a global variable, just assign nil to it.
• After that, it is as if the variable had never been used. In other
words, a global variable is existent if (and only if) it has a non-nil
value.
20
Local Variables
• Besides global variables, Lua supports local variables. We
create local variables with the local statement:
21
Local Variables
• Unlike global variables, local variables have their scope
limited to the block where they are declared.
• A block is the body of a control structure, the body of a
function, or a chunk (the file or string with the code where the
variable is declared).
22
Example
23
Reserved Words
24
and break do else elseif end false
for function if in local nil not
or repeat return then true until while
The following words are reserved; we cannot use them as
identifiers:
Operators
Various
operators and
their use
Arithmetic Operators
Consider A=10, B=20:
26
Relational Operators
Consider A=10, B=20:
27
Example
28
Logical Operators
Consider A = a week has 7 days. B = February month can have 31 days.
29
Example
30
Control Statements
Control
structures in
Lua
Control Structures
• Lua provides a small and conventional set of control
structures, with if for conditional and while and for for
iteration.
• All control structures have an explicit terminator: end
terminates the if, for and while structures.
32
if then else
• An if statement tests its condition and executes its then-part
or its else-part accordingly. The else-part is optional.
33
elseif
• When you write nested ifs, you can use elseif. It is similar to
an else followed by an if, but it avoids the need for multiple
ends:
34
while
• As usual, Lua first tests the while condition; if the condition is
false, then the loop ends; otherwise, Lua executes the body
of the loop and repeats the process.
35
Numeric for
• The for statement has two variants: the numeric for and the generic for.
• A numeric for has the following syntax:
• That loop will execute something for each value of var from exp1 to
exp2, using exp3 as the step to increment var. This third expression is
optional; when absent, Lua assumes one as the step value.
36
for var=exp1,exp2,exp3 do
something
end
Example
37
Generic for
• The generic for in Lua allows us to iterate over the values in
an iterator fashion; it is much more powerful even though it
looks simple.
• The Lua library has plenty of iterators, over which we can use
the generic for loop.
38
Generic for
• Its syntax is:
• The i in the above syntax denotes the index of the items we are
going to iterate over only by one, and the v denotes the actual
values of those items. The x is the iterable item over which we
are iterating, it can be a table, list, array, or map.
39
for i, v in pairs(x) do
...
...
end
Example
40
Functions Syntax and use
of Functions
Functions
• Syntax of functions in Lua is:
• In the syntax, a function definition has a name, a list of
parameters, and a body, which is a list of statements.
function function_name (parameters)
..
..
end
42
Example
43
Multiple Results
• Functions written in Lua also can return multiple results, by
listing them all after the return keyword.
44

Lua Programming Language.pptx

  • 1.
  • 2.
    Agenda • Introduction toLua • Data Types in Lua • Reserved Words & Variables • Operators • Control Statements • Functions 2
  • 3.
    Introduction to Lua Whatis Lua and why to use Lua?
  • 4.
    What is Lua? •Lua is a powerful, efficient, lightweight, embeddable scripting language. It supports procedural programming, OOP, functional programming, data driven programming etc. 4 4
  • 5.
    Advantages of Lua PowerfulFast • Lua is a very beginner friendly language and is easy to implement. • Lua is not a pure object-oriented language, yet it provide meta-mechanisms for implementing classes and other OOP concepts. • Lua has a safe environment, automatic memory management, and good facilities for handling strings and other kinds of data with dynamic size. • Lua has a deserved reputation for performance. • To claim to be ‘as fast as Lua’ is an aspiration of other scripting languages. 5
  • 6.
    Advantages of Lua PortableEmbeddable Lua runs on: • All flavors of Unix • Windows • On mobile devices (running Android, iOS, Windows Phone) • Embedded microprocessors (ARM and Rabbit) and even on IBM mainframes. • Lua has a simple and well documented API that allows strong integration with code written in other languages. • C, C++, C#, Java, Perl, Ruby 6
  • 7.
    Data Types inLua Data types available in Lua
  • 8.
    Data Types • Luais a dynamically typed language. • There are no type definitions in the language; each value carries its own type. • Values ​​can be stored in variables, passed as a parameter or return the results. • Lua has eight basic types are: nil, Boolean, number, string, userdata, function, thread and table. 8
  • 9.
  • 10.
    Data Types inLua nil as a kind of non-value, to represent the absence of a useful value (equivalent to false in conditional expressions). nil The Boolean type has two values, false and true. Boolean The number type represents real (double-precision floating-point) numbers. Number Strings have the usual meaning: a sequence of characters. String The table type implements associative arrays, the index of the array can be a number or a string. Tables Functions can be stored in variables, passed as arguments to other functions, and returned as results. Functions 10
  • 11.
    Tables • The tabletype implements associative arrays. • An associative array is an array that can be indexed not only with numbers, but also with strings or any other value of the language, except nil. • Moreover, tables have no fixed size; you can add as many elements as you want to a table dynamically. • Also, you can assign nil to a table field to delete it. 11
  • 12.
    Tables • To createa table a table constructor expression is used. • In its simplest form it is written as {}. • To access elements of a table [] is used. 12
  • 13.
    Nested Tables • Itis possible to implement nested tables in Lua. • To do so simply assign tables as the elements of another table. • To access the tables inside tables [][] is used. 13
  • 14.
  • 15.
    Table Manipulation • Insertionand removal of items in tables is most common in table manipulation. 15 Inserts a value into the table at specified position. table.insert (table, pos, value) Removes the value from the table. table.remove (table , pos)
  • 16.
  • 17.
  • 18.
    Variable names orIdentifiers Identifiers in Lua can be any string of letters, digits, and underscores, not beginning with a digit or other special characters; for instance: i player MAX_health _ij aVeryLongname _INPUT 18
  • 19.
    Global Variables • Globalvariables do not need declarations. You simply assign a value to a global variable to create it. • It is not an error to access a non-initialized variable; you just get the special value nil as the result. 19
  • 20.
    Global Variables • Usually,you do not need to delete global variables; if your variable is going to have a short life, you should use a local variable. • But, if you need to delete a global variable, just assign nil to it. • After that, it is as if the variable had never been used. In other words, a global variable is existent if (and only if) it has a non-nil value. 20
  • 21.
    Local Variables • Besidesglobal variables, Lua supports local variables. We create local variables with the local statement: 21
  • 22.
    Local Variables • Unlikeglobal variables, local variables have their scope limited to the block where they are declared. • A block is the body of a control structure, the body of a function, or a chunk (the file or string with the code where the variable is declared). 22
  • 23.
  • 24.
    Reserved Words 24 and breakdo else elseif end false for function if in local nil not or repeat return then true until while The following words are reserved; we cannot use them as identifiers:
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
    Logical Operators Consider A= a week has 7 days. B = February month can have 31 days. 29
  • 30.
  • 31.
  • 32.
    Control Structures • Luaprovides a small and conventional set of control structures, with if for conditional and while and for for iteration. • All control structures have an explicit terminator: end terminates the if, for and while structures. 32
  • 33.
    if then else •An if statement tests its condition and executes its then-part or its else-part accordingly. The else-part is optional. 33
  • 34.
    elseif • When youwrite nested ifs, you can use elseif. It is similar to an else followed by an if, but it avoids the need for multiple ends: 34
  • 35.
    while • As usual,Lua first tests the while condition; if the condition is false, then the loop ends; otherwise, Lua executes the body of the loop and repeats the process. 35
  • 36.
    Numeric for • Thefor statement has two variants: the numeric for and the generic for. • A numeric for has the following syntax: • That loop will execute something for each value of var from exp1 to exp2, using exp3 as the step to increment var. This third expression is optional; when absent, Lua assumes one as the step value. 36 for var=exp1,exp2,exp3 do something end
  • 37.
  • 38.
    Generic for • Thegeneric for in Lua allows us to iterate over the values in an iterator fashion; it is much more powerful even though it looks simple. • The Lua library has plenty of iterators, over which we can use the generic for loop. 38
  • 39.
    Generic for • Itssyntax is: • The i in the above syntax denotes the index of the items we are going to iterate over only by one, and the v denotes the actual values of those items. The x is the iterable item over which we are iterating, it can be a table, list, array, or map. 39 for i, v in pairs(x) do ... ... end
  • 40.
  • 41.
    Functions Syntax anduse of Functions
  • 42.
    Functions • Syntax offunctions in Lua is: • In the syntax, a function definition has a name, a list of parameters, and a body, which is a list of statements. function function_name (parameters) .. .. end 42
  • 43.
  • 44.
    Multiple Results • Functionswritten in Lua also can return multiple results, by listing them all after the return keyword. 44

Editor's Notes

  • #2  ID=d924773e-9a16-4d6d-9803-8cb819e99682 Recipe=text_billboard Type=TextOnly Variant=0 FamilyID=AccentBoxWalbaum_Zero