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

What's hot

Software development life cycle
Software development life cycleSoftware development life cycle
Software development life cycleDina Hanbazazah
 
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsQuark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsJohn De Goes
 
Comandos gerais do PHP
Comandos gerais do PHPComandos gerais do PHP
Comandos gerais do PHPPaulo Dayvson
 
Fastest Path to Dekstop, Web and Mobile Enterprise Business Application Platform
Fastest Path to Dekstop, Web and Mobile Enterprise Business Application PlatformFastest Path to Dekstop, Web and Mobile Enterprise Business Application Platform
Fastest Path to Dekstop, Web and Mobile Enterprise Business Application Platformzulmach .
 

What's hot (8)

Software development life cycle
Software development life cycleSoftware development life cycle
Software development life cycle
 
Maven Nexus
Maven NexusMaven Nexus
Maven Nexus
 
Redis Lua Scripts
Redis Lua ScriptsRedis Lua Scripts
Redis Lua Scripts
 
Aula 05 - Java Script Básico
Aula 05 -  Java Script BásicoAula 05 -  Java Script Básico
Aula 05 - Java Script Básico
 
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsQuark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
 
Aula03 - JavaScript
Aula03 - JavaScriptAula03 - JavaScript
Aula03 - JavaScript
 
Comandos gerais do PHP
Comandos gerais do PHPComandos gerais do PHP
Comandos gerais do PHP
 
Fastest Path to Dekstop, Web and Mobile Enterprise Business Application Platform
Fastest Path to Dekstop, Web and Mobile Enterprise Business Application PlatformFastest Path to Dekstop, Web and Mobile Enterprise Business Application Platform
Fastest Path to Dekstop, Web and Mobile Enterprise Business Application Platform
 

Similar to Lua Programming Language.pptx

Lambda Expressions Java 8 Features usage
Lambda Expressions  Java 8 Features usageLambda Expressions  Java 8 Features usage
Lambda Expressions Java 8 Features usageAsmaShaikh478737
 
Q-Step_WS_02102019_Practical_introduction_to_Python.pptx
Q-Step_WS_02102019_Practical_introduction_to_Python.pptxQ-Step_WS_02102019_Practical_introduction_to_Python.pptx
Q-Step_WS_02102019_Practical_introduction_to_Python.pptxnyomans1
 
Q-SPractical_introduction_to_Python.pptx
Q-SPractical_introduction_to_Python.pptxQ-SPractical_introduction_to_Python.pptx
Q-SPractical_introduction_to_Python.pptxJeromeTacata3
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryPray Desai
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_reviewEdureka!
 
python presentation.pptx
python presentation.pptxpython presentation.pptx
python presentation.pptxNightTune44
 
JSR 335 / java 8 - update reference
JSR 335 / java 8 - update referenceJSR 335 / java 8 - update reference
JSR 335 / java 8 - update referencesandeepji_choudhary
 
Java Closures
Java ClosuresJava Closures
Java ClosuresBen Evans
 
Aaa ped-2- Python: Basics
Aaa ped-2- Python: BasicsAaa ped-2- Python: Basics
Aaa ped-2- Python: BasicsAminaRepo
 
Python indroduction
Python indroductionPython indroduction
Python indroductionFEG
 
Python Programming and GIS
Python Programming and GISPython Programming and GIS
Python Programming and GISJohn Reiser
 

Similar to Lua Programming Language.pptx (20)

Mbd dd
Mbd ddMbd dd
Mbd dd
 
basics dart.pdf
basics dart.pdfbasics dart.pdf
basics dart.pdf
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
 
Lambda Expressions Java 8 Features usage
Lambda Expressions  Java 8 Features usageLambda Expressions  Java 8 Features usage
Lambda Expressions Java 8 Features usage
 
Q-Step_WS_02102019_Practical_introduction_to_Python.pptx
Q-Step_WS_02102019_Practical_introduction_to_Python.pptxQ-Step_WS_02102019_Practical_introduction_to_Python.pptx
Q-Step_WS_02102019_Practical_introduction_to_Python.pptx
 
Q-SPractical_introduction_to_Python.pptx
Q-SPractical_introduction_to_Python.pptxQ-SPractical_introduction_to_Python.pptx
Q-SPractical_introduction_to_Python.pptx
 
Ruby basics
Ruby basicsRuby basics
Ruby basics
 
Python Basics.pptx
Python Basics.pptxPython Basics.pptx
Python Basics.pptx
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
ITFT - Java
ITFT - JavaITFT - Java
ITFT - Java
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud Foundry
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
 
python presentation.pptx
python presentation.pptxpython presentation.pptx
python presentation.pptx
 
JSR 335 / java 8 - update reference
JSR 335 / java 8 - update referenceJSR 335 / java 8 - update reference
JSR 335 / java 8 - update reference
 
Java Closures
Java ClosuresJava Closures
Java Closures
 
Aaa ped-2- Python: Basics
Aaa ped-2- Python: BasicsAaa ped-2- Python: Basics
Aaa ped-2- Python: Basics
 
Unit 4 plsql
Unit 4  plsqlUnit 4  plsql
Unit 4 plsql
 
Python indroduction
Python indroductionPython indroduction
Python indroduction
 
unit1 python.pptx
unit1 python.pptxunit1 python.pptx
unit1 python.pptx
 
Python Programming and GIS
Python Programming and GISPython Programming and GIS
Python Programming and GIS
 

Recently uploaded

Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...Nguyen Thanh Tu Collection
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽中 央社
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhleson0603
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project researchCaitlinCummins3
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxCeline George
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17Celine George
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFVivekanand Anglo Vedic Academy
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMELOISARIVERA8
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaEADTU
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptxPoojaSen20
 

Recently uploaded (20)

Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 

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

  1. ID=d924773e-9a16-4d6d-9803-8cb819e99682 Recipe=text_billboard Type=TextOnly Variant=0 FamilyID=AccentBoxWalbaum_Zero