SlideShare a Scribd company logo
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

More Related Content

PPT
Unit 1 program development cycle
PPT
Data structures & problem solving unit 1 ppt
PPT
Unit 1 python (2021 r)
PDF
IGCSE & O Level Computer Workbook for P2 by Inqilab Patel
PDF
Object Oriented Programming Lab Manual
PPTX
PROBLEM SOLVING TECHNIQUES USING PYTHON.pptx
PPTX
LISP: Introduction to lisp
PPT
Analysis modeling
Unit 1 program development cycle
Data structures & problem solving unit 1 ppt
Unit 1 python (2021 r)
IGCSE & O Level Computer Workbook for P2 by Inqilab Patel
Object Oriented Programming Lab Manual
PROBLEM SOLVING TECHNIQUES USING PYTHON.pptx
LISP: Introduction to lisp
Analysis modeling

What's hot (20)

PDF
Lua - Programming Language
PPTX
The-Rapid-Application-Development-Model.pptx
PPT
Comparative Study of programming Languages
PDF
Robot Framework Dos And Don'ts
PPT
Automation testing
PPTX
Automation Testing
PPTX
Test Plan.pptx
PPTX
Cucumber BDD
PPTX
UNIT TESTING PPT
PDF
What is Web Testing?
PPTX
Swift programming language
PPTX
Chaos engineering & Gameday on AWS
PPTX
Software testing life cycle
ODP
Evolutionary process models se.ppt
PPTX
Test planning
PPTX
The Extreme Programming (XP) Model
PPT
Android ppt
PPTX
Rest API Testing
PPT
Software metrics
PPT
Mobile application development
Lua - Programming Language
The-Rapid-Application-Development-Model.pptx
Comparative Study of programming Languages
Robot Framework Dos And Don'ts
Automation testing
Automation Testing
Test Plan.pptx
Cucumber BDD
UNIT TESTING PPT
What is Web Testing?
Swift programming language
Chaos engineering & Gameday on AWS
Software testing life cycle
Evolutionary process models se.ppt
Test planning
The Extreme Programming (XP) Model
Android ppt
Rest API Testing
Software metrics
Mobile application development
Ad

Similar to Lua Programming Language.pptx (20)

PDF
Programming in Lua 3rd Edition Roberto Ierusalimschy
PDF
Programming in Lua 3rd Edition Roberto Ierusalimschy
PDF
Programming in Lua 3rd Edition Roberto Ierusalimschy
PDF
Programming In Lua 3rd Edition Roberto Ierusalimschy
PDF
Programming in Lua 2nd Edition Roberto Ierusalimschy
PDF
Programming in Lua 2nd Edition Roberto Ierusalimschy
PDF
Lua Programming Beginners Learn Lua Programming Step By Step Very Easy Rizk
PPTX
Lua Study Share
PDF
Where can buy Programming in Lua 2nd Edition Roberto Ierusalimschy ebook with...
PDF
Lua introduction
PDF
exploring-lua-a-comprehensive-guide-to-the-lightweight-programming-language.pdf
PPTX
Script up your application with Lua! -- RyanE -- OpenWest 2014
PDF
Lua زبان برنامه نویسی
PDF
Buy ebook Amateur Radio Astronomy 7th Edition John Fielding cheap price
PPTX
Use of Lua in Lab Devices
PPTX
Lua Introduction
PPTX
1003617297.pptx1003617297.pptx1003617297.pptx
PDF
Why Lua?
PDF
Hands on lua
PPTX
Lua Express Guide
Programming in Lua 3rd Edition Roberto Ierusalimschy
Programming in Lua 3rd Edition Roberto Ierusalimschy
Programming in Lua 3rd Edition Roberto Ierusalimschy
Programming In Lua 3rd Edition Roberto Ierusalimschy
Programming in Lua 2nd Edition Roberto Ierusalimschy
Programming in Lua 2nd Edition Roberto Ierusalimschy
Lua Programming Beginners Learn Lua Programming Step By Step Very Easy Rizk
Lua Study Share
Where can buy Programming in Lua 2nd Edition Roberto Ierusalimschy ebook with...
Lua introduction
exploring-lua-a-comprehensive-guide-to-the-lightweight-programming-language.pdf
Script up your application with Lua! -- RyanE -- OpenWest 2014
Lua زبان برنامه نویسی
Buy ebook Amateur Radio Astronomy 7th Edition John Fielding cheap price
Use of Lua in Lab Devices
Lua Introduction
1003617297.pptx1003617297.pptx1003617297.pptx
Why Lua?
Hands on lua
Lua Express Guide
Ad

Recently uploaded (20)

PDF
Basic Mud Logging Guide for educational purpose
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Classroom Observation Tools for Teachers
PPTX
Cell Types and Its function , kingdom of life
PDF
Pre independence Education in Inndia.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Computing-Curriculum for Schools in Ghana
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Cell Structure & Organelles in detailed.
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Pharma ospi slides which help in ospi learning
Basic Mud Logging Guide for educational purpose
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Classroom Observation Tools for Teachers
Cell Types and Its function , kingdom of life
Pre independence Education in Inndia.pdf
Final Presentation General Medicine 03-08-2024.pptx
human mycosis Human fungal infections are called human mycosis..pptx
Computing-Curriculum for Schools in Ghana
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Cell Structure & Organelles in detailed.
Anesthesia in Laparoscopic Surgery in India
GDM (1) (1).pptx small presentation for students
Sports Quiz easy sports quiz sports quiz
PPH.pptx obstetrics and gynecology in nursing
O5-L3 Freight Transport Ops (International) V1.pdf
Pharma ospi slides which help in ospi learning

Lua Programming Language.pptx

  • 2. Agenda • Introduction to Lua • Data Types in Lua • Reserved Words & Variables • Operators • Control Statements • Functions 2
  • 3. Introduction to Lua What is 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 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
  • 6. 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
  • 7. Data Types in Lua Data types available in Lua
  • 8. 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
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. 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
  • 15. 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)
  • 18. 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
  • 19. 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
  • 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 • Besides global variables, Lua supports local variables. We create local variables with the local statement: 21
  • 22. 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
  • 24. 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:
  • 29. Logical Operators Consider A = a week has 7 days. B = February month can have 31 days. 29
  • 32. 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
  • 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 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
  • 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 • 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
  • 38. 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
  • 39. 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
  • 41. Functions Syntax and use of Functions
  • 42. 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
  • 44. Multiple Results • Functions written 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