Lua - First Steps in Programming: Knut Lickert March 12, 2007
Lua - First Steps in Programming: Knut Lickert March 12, 2007
This Text shows some easy Lua-command and which result they produce. Additional information or an actual version of tis document can be found at http://lua.lickert.net This document will be lled next time.
Contents
1 Literals and output in Lua 1.1 New lines . . . . . . . . . . 1.2 Alternative String denition 1.3 Special Characters . . . . . 1.3.1 Named characters . 2 2 3 4 4 5 5 5 7 7 7 8
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
2 Operators 2.1 Basic Arithmetics . . . . . . . . . . . . . . . . . 2.2 Comparison . . . . . . . . . . . . . . . . . . . . . 2.3 Logical Operators . . . . . . . . . . . . . . . . . . 2.4 Braces in logical Expressions . . . . . . . . . . . 2.5 Braces in logical Expressions with Negation (not) 3 Variables in Lua
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
4 Branches with If 9 4.1 If-Branch with Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 4.1.1 If with multiple Ifs . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 4.2 Case-Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 5 Loops 5.1 For-Loop . 5.2 While-Loop 5.3 Repeat-loop 5.4 Abortion . . 10 10 11 11 12
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
print ( 1: Hello \ World ) print ( 2: Hello \ World ) The Result: 1: Hello World 2: Hello World Test-Programm
1 2
http://lua.lickert.net
2/14
1 2
p r i n t [ [ H e l l o World , you a r e s o n i c e today . ] ] The Result: Hello World, you are so nice today. Multiple lines are possible. Test-Programm
1 2 3
print [ [ H e l l o World , you a r e s o n i c e today . ] ] The Result: Hello World, you are so nice today. A new-line after [[ is ignored. Test-Programm
1 2 3
print [ [ H e l l o World , how [ [ n i c e ] ] a r e you today ] ] The Result: Hello World, how [[nice]] are you today The square braces may be nested.
http://lua.lickert.net
3/14
Test-Programm
1 2 3 4 5 6 7 8 9 10 11 12
When you need an overview on ASCII, you can take the ASCII Chart. In the right lower corner you nd the octal value (Source: http://dante.ctan.org/tex-archive/info/ascii.tex)
http://lua.lickert.net
4/14
2 Operators
2 Operators
2.1 Basic Arithmetics
Negative Numbers get a leading - (Minus) Binary operators: Operator Description Example + Addition a=b+c Subtraction a=b-c (negatives leading sign) * Multiplication a=b*c / Division a= b / c Potentialisation a = bc Test-Programm
1 2 3 4 5
2 2 2 2 2
+ /
3 3 3 3 3
= = = = =
, , , , ,
2 2 2 2 2
+ * /
3 3 3 3 3
) ) ) ) )
2.2 Comparison
= = Equality = Imparity < Lesser <= Less equal > Bigger >= Bigger equal Test-Programm
1 2 3
p r i n t ( 2 == 3 , 2 == 3 ) p r i n t ( 2 = 3 , 2 = 3 ) print ( 2 > 3 , 2 3 )
http://lua.lickert.net
5/14
2 Operators print ( 2 < 3 , 2 3 ) p r i n t ( 2 >= 3 , 2 = 3 ) p r i n t ( 2 <= 3 , 2 = 3 ) The Result: 2 == 3 false 2 = 3 true 2 3 false 2 3 true 2 = 3 false 2 = 3 true Test-Programm
1 2 3 4 5 6
4 5 6
A A A A A A
B B B B B B
, , , , , ,
A A A A A A
== = = =
B B B B B B
) ) ) ) ) )
a a a a a a
A A A A A A
, , , , , ,
a a a a a a
== = = =
A A A A A A
) ) ) ) ) )
http://lua.lickert.net
6/14
2 Operators
p r i n t ( t r u e and f a l s e = , t r u e and f a l s e ) p r i n t ( t r u e o r f a l s e = , t r u e o r f a l s e ) The Result: true and false = false true or false = true
2 3
p r i n t ( t r u e o r f a l s e and f a l s e = , t r u e o r f a l s e and f a l s e ) p r i n t ( t r u e o r ( f a l s e and f a l s e ) = , t r u e o r ( f a l s e and f a l s e ) ) p r i n t ( ( t r u e o r f a l s e ) and f a l s e = , ( t r u e o r f a l s e ) and f a l s e ) The Result: true or false and false = true true or (false and false) = true (true or false) and false = false
= , = , = , = , = , = ,
The Result: not true and false = false not (true and false) = true
http://lua.lickert.net
7/14
3 Variables in Lua true and not false = true not true or false = false not (true or false) = false true or not false = true
3 Variables in Lua
You must not declare variables, they are created when needed. The type of a variable is not x, it is defeined by the actual value. Variables are global, if they are not declared local. Values can be thefollowing types: Nil Numbers Literals (Characters, Words...) Boolean (true/false) Table Functionen (Special case of a block) Test-Programm
1 2 3
p r i n t ( var ) var = H e l l o World p r i n t ( var ) The Result: nil Hello World Undened variables are nil Assignment is done with = Test-Programm
1 2 3
p r i n t ( var1 , var2 ) var1 , var2 = 1 , 4 p r i n t ( var1 , var2 ) The Result: nil nil 14 Multiple assignment is possible
http://lua.lickert.net
8/14
4 Branches with If
4 Branches with If
4.1 If-Branch with Else
Test-Programm
1 2 3 4 5 6
a = 1 i f a == 1 then print ( a i s t eins ) else p r i n t ( a i s t n i c h t e i n s s onde rn , a ) end The Result: a ist eins Test-Programm
1 2 3 4 5 6
a = 999 i f a == 1 then print ( a i s t eins ) else p r i n t ( a i s t n i c h t e i n s s onde rn , a ) end The Result: a ist nicht eins sondern 999 4.1.1 If with multiple Ifs Test-Programm
1 2 3 4 5 6 7 8 9
a = 2 i f a == 1 then print ( a i s t eins ) e l s e i f a == 2 then p r i n t ( a i s t zwei ) else p r i n t ( a i s t nicht e i n s oder zwei ) print ( a i s t , a) end The Result: a ist zwei
http://lua.lickert.net
9/14
5 Loops
4.2 Case-Statement
There is no Case-statement, it must be made with if-elseif-statements.
5 Loops
5.1 For-Loop
Parameter for the For -command: 1. Initial value 2. End value 3. Inkrement Test-Programm
1 2 3
1 2 3
f o r v a r i a b l e = 0 , 1 , . 5 do print ( variable ) end The Result: 0 0.5 1 The loop value doesnt need to be a in whole numbers. Test-Programm
1 2 3
http://lua.lickert.net
10/14
5 Loops The Result: 0 0.5 1 Decrement works also. There is a special variant of the for-command for tables.
5.2 While-Loop
Test-Programm
1 2 3 4 5
i = 1 while i
= 5 do print ( i ) i = i + 1
end The Result: 1 2 3 4 5 Loop with starting condition. If the starting condition is false, the block is never executet
5.3 Repeat-loop
Test-Programm
1 2 3 4 5
http://lua.lickert.net
11/14
6 Functions Loop with condition in the end. There is at least one execution of the block.
5.4 Abortion
You can stop the loop with Break. Test-Programm
1 2 3 4 5 6 7
f o r v a r i a b l e = 1 , - 1 , - . 5 do i f v a r i a b l e == 0 then print Null e r r e i c h t break end print ( variable ) end The Result: 1 0.5 Null erreicht
6 Functions
Test-Programm
1 2 3 4 5
f u n c t i o n Test ( ) p r i n t ( H a l l o Welt ) end Test ( ) The Result: Hallo Welt Return values are dened with return. Test-Programm
1 2 3 4 5 6 7
http://lua.lickert.net
12/14
6 Functions The Result: Hallo Welt Multiple return values are possible. Test-Programm
1 2 3 4 5 6
function test () r e t u r n H a l l o , Welt end v = test () print ( v ) The Result: Hallo If you return multiple values without enough receivers, the additinal return values are ignored. Test-Programm
1 2 3 4 5
function test () r e t u r n H a l l o Welt end print ( test () ) The Result: Hallo Welt
http://lua.lickert.net
13/14
a = vorher f u n c t i o n summe( v1 , v 2 ) a = v1 + v2 return a end p r i n t ( summe( 1 , 2 ) ) print ( a ) print ( v1 ) The Result: 3 3 nil Parameter are local. Other variable are dened global. Test-Programm
1 2 3 4 5 6 7 8 9
a = vorher f u n c t i o n summe( v1 , v 2 ) l o c a l a = v1 + v2 return a end p r i n t ( summe( 1 , 2 ) ) print ( a ) print ( v1 ) The Result: 3 vorher nil If new varibales are dened with local, then they are only dened inside the block.
http://lua.lickert.net
14/14