0% found this document useful (0 votes)
44 views

Lua - First Steps in Programming: Knut Lickert March 12, 2007

The document provides an introduction to basic Lua programming concepts like literals, output, operators, variables, branches, and loops. It includes code examples and their output to demonstrate Lua syntax for various programming constructs like conditionals, arithmetic, strings, and more. The document is intended as a first steps guide for someone new to programming in the Lua language.

Uploaded by

Mikhail Miguel
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Lua - First Steps in Programming: Knut Lickert March 12, 2007

The document provides an introduction to basic Lua programming concepts like literals, output, operators, variables, branches, and loops. It includes code examples and their output to demonstrate Lua syntax for various programming constructs like conditionals, arithmetic, strings, and more. The document is intended as a first steps guide for someone new to programming in the Lua language.

Uploaded by

Mikhail Miguel
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

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

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

1 Literals and output in Lua 6 Functions 12 6.1 Functions with Parameter . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

1 Literals and output in Lua


Screen output is done with the command print. There are brackets around the parameters. Multiple parameter are separated by commas. Strings are dened with and . Test-Programm
1 2

print ( 1 : H e l l o World ) p r i n t ( 2 : H e l l o World ) The Result: 1: Hello World 2: Hello World

1.1 New lines


Test-Programm
1 2 3 4

print ( 1: Hello \ World ) print ( 2: Hello \ World ) The Result: 1: Hello World 2: Hello World Test-Programm

1 2

p r i n t ( 5 : H e l l o \n World ) p r i n t ( 6 : H e l l o \n World ) The Result: 5: Hello World 6: Hello World

http://lua.lickert.net

2/14

1 Literals and output in Lua

1.2 Alternative String denition


You can dene Strings also with [ [ and ] ]. Test-Programm
1

p r i n t [ [ H e l l o World ] ] The Result: Hello World Test-Programm

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

1 Literals and output in Lua

1.3 Special Characters


Each character can be created with \ and the octal representation1 of the ASCII-value. 1.3.1 Named characters Each character Character \a \b \f \n \r \t \v \\ \ \ \[ \] can be created with \ Function PC-Sound Backspace Form Feed New line carriage return (horizontal) tabular vertical tabular Backslash single quote double quote left square bracket right square bracket and the following short cut. Shortcut BEL BS FF LF CR HT VT

Test-Programm
1 2 3 4 5 6 7 8 9 10 11 12

- - p r i n t ( a , a ) - - p r i n t ( b , b ) - - p r i n t ( f , f ) - - p r i n t ( n , n ) - - p r i n t ( r , r ) - - p r i n t ( , ) - - p r i n t ( v , v ) p r i n t ( \\ , ) p r i n t ( \\ , \ ) p r i n t ( \\ , ) print ( \[ , \[ ) print ( \] , \] ) The Result: [ [ ] ]

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

print ( print ( print ( print ( print (

2 2 2 2 2

+ /

3 3 3 3 3

= = = = =

, , , , ,

2 2 2 2 2

+ * /

3 3 3 3 3

) ) ) ) )

The Result: 2+3=5 2 - 3 = -1 2*3=6 2 / 3 = 0.66666666666667 23=8

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

print ( print ( print ( print ( print ( print (

A A A A A A

== = > < >= <=

B B B B B B

, , , , , ,

A A A A A A

== = = =

B B B B B B

) ) ) ) ) )

The Result: A == B false A = B true A B false A B true A = B false A = B true Test-Programm


1 2 3 4 5 6

print ( print ( print ( print ( print ( print (

a a a a a a

== = > < >= <=

A A A A A A

, , , , , ,

a a a a a a

== = = =

A A A A A A

) ) ) ) ) )

The Result: a == A false a = A true a A true a A false a = A true a = A false

http://lua.lickert.net

6/14

2 Operators

2.3 Logical Operators


not Logical No and Logical and or logical or Test-Programm
1 2

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.4 Braces in logical Expressions


Test-Programm
1

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

2.5 Braces in logical Expressions with Negation (not)


Test-Programm
1 2 3 4 5 6

print ( print ( print ( print ( print ( print (

not t r u e and f a l s e not ( t r u e and f a l s e ) t r u e and not f a l s e not t r u e o r f a l s e not ( t r u e o r f a l s e ) t r u e o r not f a l s e

= , = , = , = , = , = ,

not t r u e and f a l s e ) not ( t r u e and f a l s e ) ) t r u e and not f a l s e ) not t r u e o r f a l s e ) not ( t r u e o r f a l s e ) ) t r u e o r not f a l s e )

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

f o r v a r i a b l e = 0 , 1 0 , 2 do print ( variable ) end The Result: 0 2 4 6 8 10 Test-Programm

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

f o r v a r i a b l e = 0 , 1 , . 5 do print ( variable ) end

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

i = 1 repeat print ( i ) i = i + 1 until i 5 The Result: 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

function test () r e t u r n H a l l o , Welt end v1 , v2 = t e s t ( ) p r i n t ( v1 ) p r i n t ( v2 )

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

6.1 Functions with Parameter


Test-Programm
1 2 3 4 5 6

f u n c t i o n summe( v1 , v 2 ) return ( v1 + v2 ) end p r i n t ( summe( 1 , 2 ) ) p r i n t ( summe( 2 , 3 ) ) The Result: 3 5

http://lua.lickert.net

13/14

6 Functions Parameter start with Test-Programm


1 2 3 4 5 6 7 8 9

(its a convention, not a must)

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

You might also like