Skip to content

Lua for Programmers Part 1: Language Essentials

Posted on:August 27, 2012

In this series we’ll be taking a look at most everything you should know to program in Lua. The series is targeted at people who already know how to program, and as such I’ll aim to be brief in my explanations.

Here’s an overview of the four parts the compose the series:

What is Lua?

In case you didn’t already know, Lua is an interpreted programming language. It’s fast, flexible, embeddable, simple, and easy to learn. You can get Lua from its download page.

Some Learning Resources

Two resources that I would recommend for further learning of Lua is the Programming in Lua book (they have the first edition available online) coupled with the manual. However, since Lua 5.1 is still the dominant Lua version, I’ll be referring to the 5.1 manual.

A couple other websites you might find useful are the lua-users wiki, and the Unofficial Lua FAQ.

If you’ve downloaded Lua and run it from the terminal you’ll be able to use an interactive interpreter. This is a very handy tool for learning and experimentation. Here’s an example:

$ lua
Lua 5.1.4  Copyright (C) 1994-2008, PUC-Rio
> x = 0 
> while x < 10 do 
>> x = x + 2 
>> print(x)
>> end
> return x

General Syntax

This is
a block comment

if state == 5 then
  doSomething() -- this is a line comment
elseif foo then
  a = true
  b = false

There’s a small example of Lua syntax. Lua doesn’t use semicolons, nor curly braces. However, it does have braces of a sort in the form of keywords like then and do. The code above also demonstrates conditional statements.

Variables and Types

Like many other interpreted languages, Lua is typeless, meaning that declaring a variable is as simple as var = value. This creates the variable in the global scope; we’ll soon see how to declare local variables. You can also declare multiple variables at once: var1, var2, var3 = val1, val2, val3.

There are a few constants, or, types, that you should be aware of:

  • nil is the nothing value, and also represents undefined variables. While languages such as ActionScript have both a null and undefined value, Lua combines these two things into one with nil.
  • true and false are present. Beware that in conditional statements, the only things that will equate to false are nil and false itself; everything else is considered true.

We’ll delve more into Lua’s native types in part two.


Lua has a mostly standard collection of operators; here they are in increasing precedence (source):

<     >     <=    >=    ~=    ==
+     -
*     /     %
not   #     - (unary)

Here are the differences between your standard C-based operator set:

  • The logical AND, OR, and NOT operators are the keywords and, or, and not, respectively.
  • The inequality operator is ~= instead of !=.
  • .. is the operator for string concatenation.
  • ^ raises a number to a power, for example 10 ^ 2 would yield 100 (or, ten-squared). In most other languages, this performs the binary XOR operation.
  • # is an operator used to get the length of tables and strings. We’ll see this used in part two.

If you’re wondering where bitwise operators are, there aren’t any. However, Lua 5.2 added the bit32 library which you can use for bitwise operations. If you’re using earlier versions of Lua check out this page for some solutions.

Finally, there are two things to note about assignment. First, compound assignment isn’t supported, thus an expression such as i += 3 must be written as i = i + 3. Second, the syntax of the operator itself is quite limited. You can’t do things like foo(a = 3) or a = b = 1.


You’ve already seen if statements, so let’s move on to loops. There are four different kinds in Lua.


i = 1

while i <= 5 do
  i = i + 1

Your traditional while loop. As you might guess, print is the function used to write to the standard output stream.


i = 5

  i = i - 1
until i == 1

Repeats its body until the condition is true, and is guaranteed to run the body at least once. Quite like a do..while loop.

Numeric for

Lua is a bit different when it comes to the for loop. There are two different variations of it, numeric and generic. We’ll see the generic variant in part two.

x = 1

for i = 1, 11, 2 do
  x = x * i

This loop has the following syntax:

for var = start, limit, step do
  -- code here

The equivalent while loop would be:

var = start

while var <= limit do
  -- code here
  var = var + step

Or in a C for loop:

for (int var = start; var <= limit; var += step) {}

Remember that step also takes negative numbers, so you can progress backwards too.

Loop Termination

If you want to prematurely exit from a loop you can use the break keyword:

while true do
  if condition then
    x = x ^ 2

Lua doesn’t have the continue keyword that many languages have.


function foo()
  local x, y = something(4, 5)
  return x ^ y

function something(arg1, arg2)
  local ret1 = (arg1 * arg2) ^ 2
  local ret2 = (arg1 - arg2) ^ 2
  return ret1 + ret2, ret1 * ret2

That’s how you declare functions. As you can see, the order of declaration in a file doesn’t matter as it does in some compiled languages such as C and C++. There are three things I want to point out:

  • Lua is a bit different in how local variables are declared. If you want to do so, you need to prefix the declaration with the local keyword. We’ll learn more about variable scope in part three.
  • Functions can return multiple values. foo demonstrates how to capture these values.
  • Default arguments aren’t directly supported, but there are ways of achieving the same effect which we’ll see in part three.


Well, that’s it for now. Be sure to check out part two and the other parts of the series as well. And if you have any feedback, I’d love to hear it.