Introduction to Lua Programming

Written By: S Kokila

- 31 Aug 2007 -
















Description: Lua is a powerful, light-weight programming language designed for extending applications. Coupled with it being relatively fast and having a very lenient license, it has gained a following among game developers for providing a viable scripting interface. It has been used in games such as World of Warcraft and Far Cry, and in applications such as Adobe Photoshop Lightroom and Snort.

  1. What is Lua?
  2. Getting Started
  3. Identifiers, Types, and Values
  4. Variables and Expressions
  5. Operators
  6. Statements and Assignments
  7. Control Structures
  8. File I/O

Control Structures

The control structure statements allow you to control/alter the execution of the statements based on some condition. Lua provides if for conditional and while, repeat and for for iteration/looping. The conditional expression in a control structure may return any value. nil and false makes the condition fail, any other value results in true satisfying the condition.

Condition - if then else

The if statement tests the condition and executes the then block on true or else block on false. The else block is optional. In case of nested if-else statements, elseif can be used to nest them.

-- if then else
a, b = 2, 6
if a < b then
        print("a less than b")
elseif a > b then
        print("a greater than b")
else 
        print("a equal to b")
end

Loops – while, repeat, for

The while statement allows the execution of a block of code until the condition is true.

-- while loop
i = 1
while i <= 10 do
        print(i)
        i = i + 1
end

The repeat statement also allows the execution of a block of code until the condition is true. But, the condition is tested after the execution of the block, so the block is always executed at least once.

-- repeat loop
i = 1
repeat
        print(i)
        i = i + 1
until i <= 10

The for statement has two forms: one numeric and one generic.

The numeric form has the following syntax:

for var = exp1, exp2, exp3 do block end

The loop will execute the statements in the block for each value of var from exp1 to exp2, using exp3 as the step to increment var. exp3 is optional and when absent, Lua assumes one as the step value.

-- Numeric for loop printing odd nums from 1 to 10
for i = 1, 10, 2    
do
     print(i)
end
-- i is not defined here, so its value is nil 

In the above example, the loop control variable (i), is initialized to 1 and the loop continues till its value is less than 10. At the end of each iteration, i is incremented by 2.

One point to note here is that, the loop control variable i is a local variable and is not accessible or visible outside the for loop so there is no way to access its value outside of the loop. The value of such variables might be stored in a separate variable before exiting the for loop, if necessary.

The generic for loop allows you to traverse all values returned by an iterator function. For each iteration, it calls its iteration function to produce a new value, stopping when the new value is nil. Its general syntax is:

for  in  do  end

Where could be a list of variables (with the same scoping rules of the 'i' control loop variable mentioned above). The exp results in an iterator function, a state and an initial value for the first iterator variable.

-- Numeric Iterator
for i in io.lines("mySqr.lua")
do
        print(i)
end

The io.lines is a standard library function that opens the file given as its argument and returns an iterator function that reads one line at a time from the file. The above chunk prints the mySqrt.lua program discussed earlier in this tutorial.

break and return

The break and return statements allow you to jump out from an inner block. While a return statement allows you to return results from a function, the break statement allows you to terminate a loop. break breaks the inner loop that contain it.

-- break example
i = 0
while i < 10 do
        if i == 5 then break end
        i = i + 1
end

For syntactic reasons, break and return can appear only as the last statement of a block. If you need to return or break in the middle, you need to enclose the break and return statement in an explicit block.

-- return example
function fun()
        print("Inside fun")
        print("Enter a number:")
        local num = io.read()
        if (tonumber(num) == 10) then
                print("You have entered 10");
                do return 10 end
                print("This statement is not executed")
        end
        return num
end

<< Previous

Next >>